我正在尝试在swing应用程序中使用jmathplot库。
问题是,当我将它添加到JPanel时它似乎不起作用,如下所示:
// create your PlotPanel (you can use it as a JPanel)
double[] x = new double[] { 0, 1, 2, 3, 4, 5 };
double[] y = new double[] { 10, 11, 12, 14, 15, 16 };
// create your PlotPanel (you can use it as a JPanel)
Plot2DPanel graph = new Plot2DPanel();
graph.setBounds(0, 0, 782, 272);
// add a line plot to the PlotPanel
graph.addLinePlot("my plot", x, y);
JPanel panel = new JPanel();
panel.setBounds(10, 333, 782, 272);
tab_analysis.add(panel);
panel.setLayout(null);
panel.add(graph);
因为情节没有出现,我也得到了奇怪的轴:
该网站声明:
use the PlotPanel as any Swing component (all PlotPanel extends JPanel, in fact)
我做错了什么?
答案 0 :(得分:3)
与NullLayout
一起使用,没有任何问题,但我对这个布局管理器(来自父级的委托Insets
)并不熟悉,然后我留下任何评论
简单不要使用NullLayout
覆盖getPreferredSize
Plot2DPanel
JPanel
有FLowLayout
(与NullLayout
相同的输出!在调整大小时),只接受PreferredSize
,子项无法通过其contianer调整大小,需要使用BorderLayout
/ GridLayout
作为简单图表
。
来自代码
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.math.plot.Plot2DPanel;
public class MyPlot {
private JFrame frame = new JFrame("JMathPlot library in a swing application.");
private JPanel panel = new JPanel();
public MyPlot() {
double[] x = new double[]{0, 1, 2, 3, 4, 5};
double[] y = new double[]{10, 11, 12, 14, 15, 16};
Plot2DPanel plot = new Plot2DPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
};
plot.addLinePlot("my plot", x, y); // add a line plot to the PlotPanel
panel.setLayout(new BorderLayout());
panel.add(plot);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyPlot();
}
});
}
}
答案 1 :(得分:1)
如果你想要Jean-Paul,你可以在这里看到例子: