Java GridBagConstraints

こ雲淡風輕ζ 提交于 2019-12-02 12:34:50

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

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

Generates

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...

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: The last fields gridx position has had to be increased, other wise it would actually sit over the label!

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

Finally :P - Simple as pie :D

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

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".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!