Java JPanel:无法获取JPanel的宽度

Java JPanel:无法获取JPanel的宽度,java,swing,jpanel,gridbaglayout,Java,Swing,Jpanel,Gridbaglayout,请看一下下面的代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestForm extends JFrame { private JLabel firstLabel, secondLabel; private JTextField firstTxt, secondTxt; private GridBagLayout mainLayout = new G

请看一下下面的代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestForm extends JFrame
{
    private JLabel firstLabel, secondLabel;

    private JTextField firstTxt, secondTxt;

    private GridBagLayout mainLayout = new GridBagLayout();
    private GridBagConstraints mainCons = new GridBagConstraints();
    private JPanel centerPanel;

      public TestForm()
      {
          this.setLayout(mainLayout);
        //Declaring instance variables  
        firstLabel = new JLabel("First: ");
        secondLabel = new JLabel("Second: ");

        firstTxt = new JTextField(7);
        secondTxt = new JTextField(7);


        mainCons.anchor = GridBagConstraints.WEST;
        mainCons.weightx = 1;
        mainCons.gridy = 2;
        mainCons.gridx = 1;
        mainCons.insets = new Insets(15, 0, 0, 0);
        this.add(createCenterPanel(), mainCons);

        System.out.println("Center Panel Width: "+centerPanel.getWidth());        
        this.setTitle("The Test Form");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private JPanel createCenterPanel()
    {
        centerPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        centerPanel.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,0,0,0);
        centerPanel.add(firstTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,10,0,0);
        centerPanel.add(secondLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(secondTxt,gbc);


        centerPanel.setBorder(BorderFactory.createTitledBorder("The Testing Form"));
        centerPanel.setPreferredSize(centerPanel.getPreferredSize());
        centerPanel.validate();

        System.out.println("Width is: "+centerPanel.getWidth());
        System.out.println("Width is: "+centerPanel.getHeight());

        return centerPanel;

    }


    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new TestForm();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

我试图在两个位置获得
中心面板的宽度。但在这两种情况下,我得到的答案都是0!我必须以某种方式获得宽度和高度,因为southPanel(此处不包括)将使用这些值,其中一些值会降低到高度。请帮忙

在调用
pack()
setSize()
之前,宽度为0。您可以在
pack()
call之后获取宽度并使用它


定义您自己的
LayoutManager
,它将宽度用作另一个布局零件(southPanel)的高度在调用
pack()
setSize()
之前,宽度为0。您可以在
pack()
call之后获取宽度并使用它


定义您自己的
LayoutManager
,它将宽度用作另一个布局零件(southPanel)的高度

southPanel
中有什么可以设置相对于其他随机组件的尺寸(高度较小的除外)?在
southPanel
中有什么可以设置相对于其他随机组件的尺寸(高度较低的除外)?非常感谢您的帮助!我真的很感激:)非常感谢你的帮助!我真的很感激:)