我有两张桌子和两个按钮。我希望 table-1 位于第一列并且宽度为 3 个单位,然后我希望表格右侧的两个按钮更窄(宽度为 1 个单位),我希望按钮一个在顶部,按钮 2 在底端。现在在这些按钮的右侧,我希望另一个表(表 2)再次宽 3 个单位。为此,我使用了以下约束:
//table1
c = new GridBagConstraints(0, 0, 3, 2, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
c.fill = GridBagConstraints.BOTH;
outerPanel.add(all_app_scrollpane, c);
//button1
c = new GridBagConstraints(3, 0, 1, 1, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
outerPanel.add(addButton, c);
//button2
c = new GridBagConstraints(3, 1, 1, 1, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
outerPanel.add(removeButton, c);
//table2
c = new GridBagConstraints(4, 0, 3, 2, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
c.fill = GridBagConstraints.BOTH;
outerPanel.add(curr_app_scrollpane, c);
让我们看一下table1的GridBagCONstraints。据我所知,第一个参数声明它将从 0. 列开始,宽度为 3 列(第三个参数)。然后 button1 将从第三列开始,宽度为一列。但我想我知道错了,因为结果与我预期的非常不同。
我希望这些按钮更窄,表格更宽。我怎样才能做到这一点?我想使用 GridBag 来完成这个,因为这个小面板只是一个非常复杂的 gui 的一小部分,而整个 gui 是使用 gridbag 设计的。所以我不想改变编码风格。