3

我正在尝试在摇摆应用程序中使用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)

我做错了什么?

4

2 回答 2

3
  • 与 一起工作NullLayout,没有任何问题,但我对这个布局管理器(来自父母的代表)很不熟悉Insets,然后我留下任何评论

  • 简单不要使用NullLayout

  • 覆盖getPreferredSizePlot2DPanel

  • JPanelhas (与 from ! on resizeFLowLayout的输出相同),仅接受, child 不能通过其 contianer 调整大小,需要使用/用于简单图形NullLayoutPreferredSizeBorderLayoutGridLayout

.

在此处输入图像描述 .

在此处输入图像描述

.

从代码

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();
            }
        });
    }
}
于 2013-08-19T14:05:56.767 回答
1

如果您想要 Jean-Paul,您可以在此处查看示例:

http://jmathtools.berlios.de/doku.php?id=jmathplot:tutorial

于 2013-08-19T14:09:06.550 回答