Java 如何在JPanel中使用JFrame和jmathplot图

Java 如何在JPanel中使用JFrame和jmathplot图,java,swing,jframe,jpanel,preferredsize,Java,Swing,Jframe,Jpanel,Preferredsize,注意:此问题是来自的后续问题 我刚刚从我的最后一个问题中学到了以下几点: JPanel具有FLowLayout(与NullLayout!在调整大小时具有相同的输出),仅接受首选大小,子级不能使用其容器调整大小,需要使用边框布局/网格布局,用于简单图形 还演示了这一原则: import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; imp

注意:此问题是来自的后续问题

我刚刚从我的最后一个问题中学到了以下几点:

JPanel
具有
FLowLayout
(与
NullLayout
!在调整大小时具有相同的输出),仅接受
首选大小
,子级不能使用其容器调整大小,需要使用
边框布局
/
网格布局
,用于简单图形

还演示了这一原则:

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();
            }
        });
    }
}
现在我想要实现的是将图形集成到父JPanel中,以便在swing应用程序中使用它

这方面的设想是:

如何在我的“父”JPanel中实现图形,而不违反使其正常工作的约束条件

(我进行了调查,但没有找到解决问题的正确方法)


如果JInternalFrame确实是解决方案,我应该如何使用它?(请提供代码)

基本都是些东西,没有什么特别的,大多数windows应用程序都是类似的设计

  • JFrame(API中的BorderLayout)包含两个JPANEL

  • 第一个JPanel(网格布局),带有两个组件(JFrame.WEST/EAST)

    • JScrollPane中的JTree/JList,用于显示保存在util.List或Tree/Map中的泛型选项

    • 将放置一个面板

  • 带绘图的JPanel(JFrame.CENTER)

  • 您可以对两个JPanel(a.m.)使用JSplitPane,其中包含隐藏、禁用或标准分隔符


我不知道这是否是您正在寻找的,但我将为您提供此源代码,也许它将帮助您:

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


}

}

为什么不看看,对于这个任务+1、既然你从上一篇博文中学到了很多东西,毫无疑问,这位天才自己回答得很好:——)@nIcEcOw你的评论令人费解。你是说JInternalFrame确实拥有解决方案吗?是的,我想是的,尽管我到目前为止从未接触过
JInternalFrame
,尽管正如你所描述的问题,它们在我看来适合这类任务。这就是为什么我发布了一条评论而不是答案:-)如果JFrame必须具有绝对布局呢?那么你必须计算放置在JFrame中的JComponents的内部面积,假设它是关于基本代数的好吧,假设我在我的图片中有10个随机JPanel,这样父JPanel具有绝对(XY)布局。现在我该如何使用JComponents呢?(我不明白你对基础代数的意思)我不明白你对基础代数的意思==从容器插入到与孩子一起处理空布局的基本调用时间比学习/尝试标准或自定义布局管理器要长,特别是你可以确定哪一个是可调整的,如何调整大小,GUI中的比率等等。