1

注意:这个问题是这个问题的后续问题

我刚刚从上一个问题中了解到以下内容:

JPanelhas (具有与 from ! on resizeFLowLayout相同的输出),仅接受, child 不能通过其容器调整大小,需要使用/用于简单图形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;
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();
            }
        });
    }
}

现在我想要实现的是将图形集成到 aparent JPanel中以在我的摇摆应用程序中使用它。

对此的可视化将是:

在此处输入图像描述

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

(我查看了JInternalFrame但无法为我的问题提出正确的实现)

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

4

2 回答 2

2

bacis 的东西,没什么特别的,大多数应用程序窗口都是类似的设计

  • 持有两个 JPanel 的 JFrame(API 中的 BorderLayout)

    1. 第一个 JPanel (GridLayout) 有两个组件 (JFrame.WEST/EAST)

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

      • 将放置randomPanel

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

  • 您可以将 JSplitPane 用于两个具有隐藏、禁用或标准 Divider 的 JPanel (am)

于 2013-08-19T17:26:51.570 回答
1

我不知道这是否是你正在寻找的,但我会给你这个源代码,也许它会帮助你:

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


}

}

于 2014-05-05T11:00:08.023 回答