简体   繁体   English

如何在JPanel中添加带有jmathplot图的JFrame

[英]How to put JFrame with jmathplot graph in JPanel

Note : this question is a follow up question from this question . 注意 :这个问题是这个问题的后续问题

I have just learned the following from my last question: 我刚刚从上一个问题中了解到以下内容:

JPanel has FLowLayout (with the same output as from NullLayout ! on resize), accepting only PreferredSize , child aren't resizable with its container, is required to use BorderLayout / GridLayout for simple graph JPanel具有FLowLayout (具有相同的输出作为从NullLayout !上调整大小),只接受PreferredSize ,儿童是不可调整大小与它的容器,是需要使用BorderLayout / GridLayout为简单图

A demonstration of this principle was also given: 还给出了这一原则的证明:

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;
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();
            }
        });
    }
}

Now what I want to achieve is to integrate the graph into a parent JPanel to use it in my swing application. 现在我想要实现的是将图形集成到parent JPanel以在我的swing应用程序中使用它。

A visualisation of this would be: 对此的可视化将是:

在此输入图像描述

How do I implement the graph in my 'parent' JPanel without violating the constraints for making it work properly? 如何在我的“父级”JPanel中实现图形而不违反使其正常工作的约束?

(I looked into JInternalFrame but couldn't come up with a correct implementation for my problem) (我查看了JInternalFrame,但无法为我的问题找到正确的实现)

If JInternalFrame is indeed the solution, how should I use it? 如果JInternalFrame确实是解决方案,我应该如何使用它? (Please supply code) (请提供代码)

bacis stuff, nothing special, most of apps windows is similair desingned bacis的东西,没有什么特别的,大多数应用程序窗口是similair desingned

  • JFrame (BorderLayout in API) holding two JPanels JFrame(API中的BorderLayout)持有两个JPanel

    1. 1st JPanel (GridLayout) with two components (JFrame.WEST/EAST) 第一个带有两个组件的JPanel(GridLayout)(JFrame.WEST / EAST)

      • JTree/JList in JScrollPane for displaying generics options saved in util.List or Tree/Map JScrollPane中的JTree / JList,用于显示保存在util.List或Tree / Map中的泛型选项

      • there will be placed randomPanel 将会放置randomPanel

    2. JPanel with plot (JFrame.CENTER) 带绘图的JPanel(JFrame.CENTER)

  • you can to use JSplitPane for two JPanels (am) with hidden, disables, or standard Divider 你可以使用JSplitPane两个JPanels(am)隐藏,禁用或标准分割器

i don't know if that's what you're looking about but i will give you this source code, maybe it will help you: 我不知道你是否正在寻找这个,但我会给你这个源代码,也许它会帮助你:

public class dz extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                dz frame = new dz();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public dz() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 917, 788);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    SpringLayout sl_contentPane = new SpringLayout();
    contentPane.setLayout(sl_contentPane);

    // define your data
    double[] x = { 0, 1, 2, 3, 4, 5 };
    double[] y = { 45, 89, 6, 32, 63, 12 };

    // create your PlotPanel (you can use it as a JPanel)
    Plot2DPanel plot = new Plot2DPanel();

    // define the legend position
    plot.addLegend("SOUTH");

    // add a line plot to the PlotPanel
    plot.addLinePlot("my plot", x, y);




    sl_contentPane.putConstraint(SpringLayout.EAST, plot, 600, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.NORTH, plot, 15, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, plot, 15, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, plot, 600, SpringLayout.NORTH, contentPane);

/*  panel.setLayout(new GridBagLayout());
    panel.add(plot);
    plot.validate();
    plot.repaint();
    plot.setBounds(50,50,100,100);*/

    contentPane.add(plot);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLACK);
    sl_contentPane.putConstraint(SpringLayout.NORTH, panel, 20, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST, panel, 16, SpringLayout.EAST, plot);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, panel, 408, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.EAST, panel, 251, SpringLayout.EAST, plot);
    contentPane.add(panel);


}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM