2

请看下面的代码

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

    }
}

当我运行这个程序时,我得到以下信息。

这就是我得到的

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

但是,这不是我期望的 JLabels 之间的差距,除了bfPercentageLabel. 如果我删除它bfPercentageLabel,间距问题就消失了,它变得正常,如下图所示。

这是我想要的这些 JLabel 之间的差距

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

4

1 回答 1

7

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

gbc.gridwidth = 6;
于 2013-01-07T15:08:14.537 回答