Java 在交替列GridBagLayout中设置组件
是否有一种方法可以在交替列0、2、4、6中设置组件,而不使用GridBagLayout中的空组件填充1、3、5,使用GridBagConstraints 这里的Java示例将第5个按钮设置为从第1列开始,但可能是因为上面的行已经设置好了?Java 在交替列GridBagLayout中设置组件,java,swing,Java,Swing,是否有一种方法可以在交替列0、2、4、6中设置组件,而不使用GridBagLayout中的空组件填充1、3、5,使用GridBagConstraints 这里的Java示例将第5个按钮设置为从第1列开始,但可能是因为上面的行已经设置好了? 有没有一种方法可以在GridBagLayout中设置交替列0、2、4、6中的组件,而不使用空组件填充1、3、5 不。如果没有为单元定义组件,则其大小基本上为0 布局无法猜测“空列”的大小 也许您可以使用“insets”约束来提供列之间的空间 有没有一种方法可
有没有一种方法可以在GridBagLayout中设置交替列0、2、4、6中的组件,而不使用空组件填充1、3、5 不。如果没有为单元定义组件,则其大小基本上为0 布局无法猜测“空列”的大小 也许您可以使用“insets”约束来提供列之间的空间 有没有一种方法可以在GridBagLayout中设置交替列0、2、4、6中的组件,而不使用空组件填充1、3、5 不。如果没有为单元定义组件,则其大小基本上为0 布局无法猜测“空列”的大小
也许您可以使用“insets”约束来提供列之间的空间。您可以使用行高和列宽定义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));
您可以使用行高和列宽定义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));