在 NetBeans GUI Builder 中调整 JPanel 表单的大小 [英] Resizing JPanel Form in NetBeans GUI Builder

查看:26
本文介绍了在 NetBeans GUI Builder 中调整 JPanel 表单的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Netbeans 中有一个 JPanel 表单,上面有 Jlists、JTextFields 和 JPanels.我想将 JFrame 分成四个(或六个或八个......)部分并将这个 JPanel 表单放入每个部分.我在这里想象了我想做的工作.

I have a JPanel Form in Netbeans and it has Jlists, JTextFields and JPanels on it. I want to divide the JFrame into four (or six or eight...) parts and place this JPanel Form into each of them. I visualized the job i want to do here.

我尝试了 BorderLayout、GridLayout 和 GridBagLayout.然而,在它们中的任何一个中,JPanel 和组件(Jlists、JTextFields 和 JPanels)都不会改变它们的大小并且只显示其中的一部分.

I tried BorderLayout, GridLayout and GridBagLayout. However in any of them the JPanel and the components(Jlists, JTextFields and JPanels on it) don't change their size and shows only a part of it.

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

推荐答案

我相信这段代码片段将帮助您实现您想要实现的目标.理解一个组件只能有 1 个父级(即只添加一次),并且多次将其添加到同一个 Container 与删除它并再次添加它是一样的

I believe this code snippet will help you to achieve what you are trying to achieve. Understand that a Component can only have 1 parent (ie added only once) and that adding it multiple times to the same Container is the same as removing it and adding it again

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

这篇关于在 NetBeans GUI Builder 中调整 JPanel 表单的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆