GridBagLayout中的间距问题

时间:2013-01-07 15:02:21

标签: java swing jpanel jlabel gridbaglayout

请查看以下代码

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

public class TestForm extends JFrame
{
    private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel,bfPercentageLabel;

    private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;

    private JPanel centerPanel;


      public TestForm()
      {
        //Declaring instance variables  
        heightLabel = new JLabel("Height: ");
        weightLabel = new JLabel("Weight: ");
        waistLabel = new JLabel("Waist: ");
        neckLabel = new JLabel("Neck: ");
        hipsLabel = new JLabel("Hips: ");        
        bfPercentageLabel = new JLabel("The Orginal Test Score Is: ");


        heightTxt = new JTextField(7);
        weightTxt = new JTextField(7);
        waistTxt = new JTextField(7);
        neckTxt = new JTextField(7);
        hipsTxt = new JTextField(7);



        this.add(createCenterPanel(),"Center");
        this.add(new JPanel(),"West");
        this.add(new JPanel(),"East");
        this.setTitle("The Test Form");
        this.setResizable(false);
        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(heightLabel,gbc);

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

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

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

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

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

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

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

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

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



        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(50,0,0,0);
        centerPanel.add(bfPercentageLabel,gbc);



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

        centerPanel.setPreferredSize(centerPanel.getPreferredSize());
        centerPanel.validate();

        return centerPanel;

    }

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

    }
}

当我运行此程序时,我得到以下内容。

This is what I get

在那里,您可以看到heightLabelheightTxt之间的距离。并waistLabelwaistTxt。这个巨大的差距是因为JLabel bfPercentageLabel。它包含许多字母,然后是其他字母,因此它使这个间隙适合bfPercentageLabel宽度。

但是,除了bfPercentageLabel之外,这不是我期望的JLabel之间的差距。如果删除bfPercentageLabel,则间距问题就会消失,并且会变为正常,如下图所示。

This is the gap I want between these JLabels

我希望所有JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel和JTextFields heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt保持相同的格式,使用与第二张图片中显示的相同的间距,即使bfPercentageLabel的宽度更大。让bfPercentageLabel拥有所需的空间,让其他人保持原样。我怎样才能做到这一点?请帮忙!

1 个答案:

答案 0 :(得分:7)

对于您的上一个标签(长标签),只需将以下内容添加到GridBagConstraint

即可
gbc.gridwidth = 6;
相关问题