0

我在 JPanel 中有 5 个组件。我添加的前 4 个组件看起来一切都很顺利。但是,当我尝试向 JPanel 添加第 5 个组件时,组件之间的间距会因任何原因发生变化!

没有第 5 个组件:

First Name: [..............]

Last Name: [..............]

和:

First Name:---------------- [.............]

Last Name:----------------- [.............]

What is your favorite sport:

假装标签和文本字段之间的破折号是空格

标签和文本字段之间的间距发生了变化!这是我的代码,请帮助我!

    public static void main(String[] args)
{
    JFrame frame = new JFrame("Survey");
    frame.setSize(800, 600);
    frame.setLayout(new FlowLayout(FlowLayout.CENTER));
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p1 = new JPanel(new GridBagLayout());
    //LINE.START = Top Left Corner
    frame.add(p1);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5, 5, 10, 10);

    JLabel lblFirstN = new JLabel("First Name:");
    JLabel lblLastN = new JLabel("Last Name:");
    JLabel lblFavSport = new JLabel("What is your favorite sport:");

    JTextField txtFirstN = new JTextField();
    txtFirstN.setPreferredSize(new Dimension(100, 20));

    JTextField txtLastN = new JTextField();
    txtLastN.setPreferredSize(new Dimension(100, 20));

    gbc.gridx = 0;
    gbc.gridy = 0;
    p1.add(lblFirstN, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    p1.add(txtFirstN, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    p1.add(lblLastN, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    p1.add(txtLastN, gbc);

    //this block of code is what is screwing me
    gbc.gridx = 0;
    gbc.gridy = 2;
    p1.add(lblFavSport, gbc);
}
4

2 回答 2

3

您获得此输出的原因是,这就是布局管理器的设计方式。

GridBagLayout是 的(虚拟)扩展GridLayout。事实上,它把它的组件放在一个网格中,但比GridLayout. 在包含的布局管理器中,您将获得类似于 HTML 表格的内容。

让我们仔细看看。以下代码...

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);

生成

在此处输入图像描述

很明显,您可以看到行和列。每行具有相同的高度,每列具有相同的宽度......

现在,如果我们添加下一个标签和字段......

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridy++;
add(new JLabel("What is your favorite sport:"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);

这是您当前的问题...

在此处输入图像描述

您可以看到第一列的宽度已增加以适应新标签...

现在我们可以通过使用gridWidth...

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridy++;
gbc.gridwidth = 2;
add(new JLabel("What is your favorite sport:"), gbc);

gbc.gridwidth = 1;
gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
gbc.gridx = 2;
gbc.gridy++;
add(new JTextField(15), gbc);

gridx注意:必须增加最后一个字段的位置,否则它实际上会位于标签上方!

在此处输入图像描述

现在,这更好,但是,我认为这不是您想要的:P

我们还需要调整前两个字段以跨越到下一个单元格......

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("First Name"), gbc);
gbc.gridy++;
add(new JLabel("Last Name"), gbc);
gbc.gridy++;
gbc.gridwidth = 2;
add(new JLabel("What is your favorite sport:"), gbc);

gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 0;
add(new JTextField(15), gbc);
gbc.gridy++;
add(new JTextField(15), gbc);
gbc.gridx = 2;
gbc.gridy++;
add(new JTextField(15), gbc);

在此处输入图像描述

最后 :P - 像馅饼一样简单 :D

您可能需要仔细查看如何使用 GridBagLayout了解更多详细信息...

于 2013-04-13T00:14:21.507 回答
1

The reason that the last JLabel is not aligned with the previous 2 is that all labels are center aligned and appear in the same column. When there are only 2 labels, they appear aligned due to the happy coincidence that their FontMetric widths are equal. Add a third and the labels appear staggered.

To fix, you can anchor the labels to counter this effect by anchoring the components to GridBagConstraints.WEST and set the weight along the x axis:

gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;

This has the effect of left aligning component within their GridBagLayout "cells".

于 2013-04-12T21:28:21.077 回答