注意:此问题是来自this question的后续问题。
我刚从上一个问题中了解到以下内容:
JPanel
有FLowLayout
(与NullLayout
相同的输出!在调整大小时),仅接受PreferredSize
,子项无法使用其容器调整大小,是必需的将BorderLayout
/ GridLayout
用于简单图表
还给出了这一原则的证明:
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();
}
});
}
}
现在我想要实现的是将图形集成到parent JPanel
以在我的swing应用程序中使用它。
对此的可视化将是:
如何在我的“父级”JPanel中实现图形而不违反使其正常工作的约束?
(我调查了JInternalFrame,但无法为我的问题找到正确的实施方案)
如果JInternalFrame确实是解决方案,我应该如何使用它? (请提供代码)
答案 0 :(得分:2)
bacis的东西,没什么特别的,大多数应用程序窗口都是similair desingned
持有两个JPanels的JFrame(API中的BorderLayout)
第一个带有两个组件的JPanel(GridLayout)(JFrame.WEST / EAST)
JScrollPane中的JTree / JList,用于显示保存在util.List或Tree / Map中的泛型选项
会放置randomPanel
JPanel with plot(JFrame.CENTER)
您可以将JSplitPane用于两个带隐藏,禁用或标准分隔符的JPanels(a.m。)
答案 1 :(得分: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);
}
}