Java GridBagConstraints [英] Java GridBagConstraints

查看:119
本文介绍了Java GridBagConstraints的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 5 components in a JPanel. Everything is looking smooth with the first 4 component's I've added. However, when I try adding a 5th component to the JPanel, the spacing between the components change for whatever reason!

没有第5个组件:

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

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

使用:

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

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

What is your favorite sport:

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

Pretend the dashes above between label and textfield is space

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

The spacing between the labels and textfield change! Here is my code, please help me!

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


推荐答案

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

The reason your getting this output is because, that's the way the layout manager is designed.

GridBagLayout 是<的一个(虚拟)扩展code> GridLayout的。事实上,它将它的组件放在网格中,但比 GridLayout 更灵活。关闭,您将获得包含在布局管理器中的HTML表格。

GridBagLayout is a (virtual) extension of the GridLayout. In the fact that it lays it's components out in a grid, but is far more flexible then the GridLayout. It is the closes you will get to something like a HTML table in the included layout managers.

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

Lets take closer look. The following code...

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

生成

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

Clearly, you can see the rows and columns. Each row has the same height and each column has the same width...

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

Now, if we add the next label and field...

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

这是您目前的问题......

And here is your current problem...

你可以看到第一列的宽度已经增加以容纳新标签......

You can see that the first column's width has increased to accommodate the new label...

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

Now we can fix this by using 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);

nb:最后一个字段 gridx 位置必须增加,否则它实际上会超过标签!

nb: The last fields gridx position has had to be increased, other wise it would actually sit over the label!

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

Now, that's better, but, I don't think that's quite what you want :P

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

We need to adjust the first two fields to span into the next cell as well...

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

Finally :P - Simple as pie :D

您可以想进一步了解如何使用GridBagLayout 更多细节......

You may want to take a closer look at How to use GridBagLayout for more details...

这篇关于Java GridBagConstraints的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆