我在Netbeans中有一个JPanel表单,它上面有Jlists,JTextFields和JPanels。我想将JFrame分成四个(或六个或八个......)部分,并将这个JPanel表单放入其中。我想象我想做的工作here。
我尝试过BorderLayout,GridLayout和GridBagLayout。但是在其中任何一个中,JPanel和组件(Jlists,JTextFields和JPanels)都不会改变它们的大小,只显示它的一部分。
public class DynamicLineAndTimeSeriesChart extends ApplicationFrame implements ActionListener {
private TimeSeries series;
private double lastValue = 100.0;
private Timer timer = new Timer(10000, this);
GaugePanel panel; //*****That's the JPanel form I created by using NetBeans*****
//Note that GaugePanel extends javax.swing.JPanel
final ChartPanel chartPanel;//*****Since I need an xy-graph, I import jfree library
private Timer timer2 = new Timer(10000, this);
GaugePanel panel2;
final ChartPanel chartPanel2;
/**
* Constructs a new dynamic chart application.
*
* @param title the frame title.
*/
public DynamicLineAndTimeSeriesChart(final String title) throws IOException {
super(title);
JFrame.setDefaultLookAndFeelDecorated(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize.width, screenSize.height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.series = new TimeSeries("Random Data", Millisecond.class);
JPanel bigpanel=new JPanel();
GridLayout experimentLayout = new GridLayout(2,2);
bigpanel.setLayout(experimentLayout);
final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
final JFreeChart chart = createChart(dataset);
timer.setInitialDelay(1000);
chart.setBackgroundPaint(Color.LIGHT_GRAY);
panel = new GaugePanel();
panel.init();
chartPanel = new ChartPanel(chart);
panel.getjPanel1().add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(367, 336));
bigpanel.add(panel);
timer.start();
panel.setVisible(true);
chartPanel.setVisible(true);
final TimeSeriesCollection dataset2 = new TimeSeriesCollection(this.series);
final JFreeChart chart2 = createChart(dataset2);
timer2.setInitialDelay(1000);
chart2.setBackgroundPaint(Color.LIGHT_GRAY);
panel2 = new GaugePanel();
panel2.init();
chartPanel2 = new ChartPanel(chart2);
panel2.getjPanel1().add(chartPanel2);
chartPanel2.setPreferredSize(new java.awt.Dimension(367, 336));
bigpanel.add(panel2);
timer2.start();
panel2.setVisible(true);
chartPanel2.setVisible(true);
setContentPane(bigpanel);
repaint();
setVisible(true);
}
答案 0 :(得分:1)
我相信这段代码会帮助您实现您想要实现的目标。了解一个组件只能有一个父组件(即只添加一个组件),并且将多次添加到同一个Container
与删除它并再次添加它相同
JFrame f = new JFrame("Test");
f.getContentPane().setLayout(new GridLayout(2,2));
Color[] colors = new Color [Color.RED, COLOR.GREEN, COLOR.BLUE, COLOR.BLACK];
for (int i = 0; i < 4; i++) {
JPanel jp = new JPanel();
jp.setBackground(colors[i];
f.getContentPane().add(jp);
}