请看下面的代码
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
。和waistLabel
和waistTxt
。这个巨大的差距是因为 JLabel bfPercentageLabel
。它包含很多字母,然后是其他字母,因此它使这个间隙适合bfPercentageLabel
宽度。
但是,这不是我期望的 JLabels 之间的差距,除了bfPercentageLabel
. 如果我删除它bfPercentageLabel
,间距问题就消失了,它变得正常,如下图所示。
我希望所有 JLabelsheightLabel, weightLabel, waistLabel, neckLabel, hipsLabel
和 JTextFieldsheightTxt, weightTxt, waistTxt, neckTxt, hipsTxt
保持相同的格式,使用第二张图像中显示的相同间距,即使宽度bfPercentageLabel
更大。让bfPercentageLabel
他们拥有它需要的空间,但让其他人保持原样。我怎样才能做到这一点?请帮忙!