GridBagLayout 的奇怪行为,部分忽略了网格位置
Odd behavior with GridBagLayout, grid positions partially ignored
我在使用 GridBagLayout 时遇到了一个非常奇怪的问题,其中布局看似 "adjusts" 网格位置以使组件在整齐的列中对齐 - 这正是 我不 想要的.我希望我的组件以砖墙方式对齐,但我得到的是棋盘布局。
本例重现案例:
public class GBLayout extends JPanel {
public GBLayout() {
setLayout(new GridBagLayout());
for (int y=0; y<10; ++y) {
int offset = y & 1;
if (offset != 0) {
GridBagConstraints c = new GridBagConstraints(
0, y, // x, y
1, 1, // w, h
0D, 0D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(new JLabel("!"), c);
}
for (int x=0; x<10; ++x) {
int bx = x * 2 + offset;
int by = y;
JButton b = new JButton("(" + bx + ", " + by + ")");
GridBagConstraints c = new GridBagConstraints(
bx, by, // x, y
2, 1, // w, h
1D, 0D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(b, c);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GBLayout panel = new GBLayout();
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
这是目前的样子:
预期布局是一堵砖墙(大约,我手动编辑的):
那只是在最左边的列中遵守的,在那之后 GridBagLayout 似乎对我的组件的位置有自己的想法。是什么原因造成的,我该如何解决?
这会给你想要的效果,在我的电脑上它看起来像这样:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GBLayout extends JPanel {
public GBLayout() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWeights = new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
setLayout(gridBagLayout);
for (int y=0; y<10; ++y) {
int offset = y & 1;
if (offset != 0) {
GridBagConstraints c = new GridBagConstraints(
0, y, // x, y
1, 1, // w, h
0D, 0D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(new JLabel("!"), c);
}
for (int x=0; x<10; ++x) {
int bx = x * 2 + offset;
int by = y;
JButton b = new JButton("(" + bx + ", " + by + ")");
GridBagConstraints c = new GridBagConstraints(
bx, by, // x, y
2, 1, // w, h
1D, 1D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(b, c);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GBLayout panel = new GBLayout();
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
诀窍是将 table columnWeights 设置为 1,因为如果没有它,colums 可以有不同的大小,一些列可以拉伸到按钮宽度,或者粉碎到 0。
我在使用 GridBagLayout 时遇到了一个非常奇怪的问题,其中布局看似 "adjusts" 网格位置以使组件在整齐的列中对齐 - 这正是 我不 想要的.我希望我的组件以砖墙方式对齐,但我得到的是棋盘布局。
本例重现案例:
public class GBLayout extends JPanel {
public GBLayout() {
setLayout(new GridBagLayout());
for (int y=0; y<10; ++y) {
int offset = y & 1;
if (offset != 0) {
GridBagConstraints c = new GridBagConstraints(
0, y, // x, y
1, 1, // w, h
0D, 0D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(new JLabel("!"), c);
}
for (int x=0; x<10; ++x) {
int bx = x * 2 + offset;
int by = y;
JButton b = new JButton("(" + bx + ", " + by + ")");
GridBagConstraints c = new GridBagConstraints(
bx, by, // x, y
2, 1, // w, h
1D, 0D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(b, c);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GBLayout panel = new GBLayout();
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
这是目前的样子:
预期布局是一堵砖墙(大约,我手动编辑的):
那只是在最左边的列中遵守的,在那之后 GridBagLayout 似乎对我的组件的位置有自己的想法。是什么原因造成的,我该如何解决?
这会给你想要的效果,在我的电脑上它看起来像这样:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GBLayout extends JPanel {
public GBLayout() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWeights = new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
setLayout(gridBagLayout);
for (int y=0; y<10; ++y) {
int offset = y & 1;
if (offset != 0) {
GridBagConstraints c = new GridBagConstraints(
0, y, // x, y
1, 1, // w, h
0D, 0D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(new JLabel("!"), c);
}
for (int x=0; x<10; ++x) {
int bx = x * 2 + offset;
int by = y;
JButton b = new JButton("(" + bx + ", " + by + ")");
GridBagConstraints c = new GridBagConstraints(
bx, by, // x, y
2, 1, // w, h
1D, 1D,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2),
0, 0);
add(b, c);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GBLayout panel = new GBLayout();
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
诀窍是将 table columnWeights 设置为 1,因为如果没有它,colums 可以有不同的大小,一些列可以拉伸到按钮宽度,或者粉碎到 0。