是否有一种方法可以在交替列0、2、4、6中设置组件,而不使用GridBagLayout中使用GridBagConstraints填充1、3、5中的空组件?
这里的Java示例将第5个按钮设置为从第1列开始,但可能是因为上面的行已经设置好了吗?
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
发布于 2017-04-05 19:53:40
是否有一种方法可以在交替列0、2、4、6中设置组件,而不使用GridBagLayout中的空组件填充1、3、5
不是的。如果没有为一个单元定义组件,那么它的大小基本上是0。
布局无法猜测“空列”的大小。
也许您可以使用“inset”约束在列之间留出空间。
发布于 2017-04-06 10:16:23
可以用行高和列宽定义GridBagLayout。
int[] rowHeights = new int[] { 50, 50, 50};
int[] columnWidths = new int[] { 100, 100, 100, 100, 100};
GridBagLayout layout = new GridBagLayout();
layout.rowHeights = rowHeights;
layout.columnWidths = columnWidths;使用GridBagConstraint添加组件,如下所示
JPanel panel = new JPanel();
panel.setLayout(layout);
JLabel label1 = new JLabel("Label 1");
JLabel label2 = new JLabel("Label 2");
JLabel label3 = new JLabel("Label 3");
panel.add(label1, new GridBagConstraints(
0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0,0, 0, 0), 0, 0));
panel.add(label2, new GridBagConstraints(
2, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0,0, 0, 0), 0, 0));
panel.add(label3, new GridBagConstraints(
4, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0,0, 0, 0), 0, 0));https://stackoverflow.com/questions/43240363
复制相似问题