问题描述
我正在尝试在摆动应用程序中使用 jmathplot 库.
I'm trying to use the jmathplot library in a swing application.
问题是,当我按如下所示将其添加到JPanel时,它似乎不起作用:
Problem is, it doesn't seem to work when I add it to a JPanel as follows:
// 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);
因为该图未显示,并且我也得到了奇怪的轴:
Because the plot doesn't show up and I get weird axes as well:
该网站指出:
use the PlotPanel as any Swing component (all PlotPanel extends JPanel, in fact)
我该怎么办?
推荐答案
-
可以与
NullLayout
一起使用,没有任何问题,但是我对这个布局管理器并不熟悉(从父级中删除Insets
),然后我会留下关于works with
NullLayout
, without any issue, but I'm quite not familair with this layout manager (delegateInsets
from parent), then I'm leaving any comments about简单不要使用
NullLayout
为
Plot2DPanel
JPanel
具有FLowLayout
(在调整大小时与NullLayout
!的输出相同),仅接受PreferredSize
,子对象的容器大小不可调整,必须使用BorderLayout
/GridLayout
用于简单图形JPanel
hasFLowLayout
(with the same output as fromNullLayout
! on resize), accepting onlyPreferredSize
, child aren't resizable with its contianer, is required to useBorderLayout
/GridLayout
for simple graph.
.
.
来自代码
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(); } }); } }
这篇关于jmathplot在swing应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!