1

次のコードを見てください

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

centerPanel2か所で幅を取得しようとしています。しかし、どちらの場合も、答えは 0 です。southPanel (ここには含まれていません) がこれらの値を使用し、一部は高さを減らすため、幅と高さを取得する必要があります。助けてください!

4

1 に答える 1

4

pack()orsetSize()が呼び出される前の幅は 0です。幅を取得してpack()呼び出し後に使用できます

また

LayoutManager別のレイアウト パーツ (southPanel) の高さの幅を使用する独自の定義

于 2013-01-08T16:48:05.107 に答える