gpt4 book ai didi

java - GridBagLayout 中的间距问题

转载 作者:行者123 更新时间:2023-12-02 07:38:03 27 4
gpt4 key购买 nike

请看下面的代码

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

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

最佳答案

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

gbc.gridwidth = 6;

关于java - GridBagLayout 中的间距问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com