请看下面的代码
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
宽度。但是,除了
bfPercentageLabel
,这不是我期望的JLabel之间的差距。如果我删除该bfPercentageLabel
,间距问题就消失了,它变得正常,如下图所示。我希望所有JLabels
heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel
和JTextFields heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt
保持相同的格式,使用第二个图像中所示的相同间距,即使bfPercentageLabel
的宽度更大。让bfPercentageLabel
拥有所需的空间,但让其他人保持原样。我怎样才能做到这一点?请帮忙! 最佳答案
对于您的最后一个标签(长标签),只需将以下内容添加到GridBagConstraint
中:
gbc.gridwidth = 6;