Java Swing:GridBagLayout,如何将4个按钮挤到两列?
所以在我的 japanel 中,我有一个 2 列宽的组件。我想在底部添加 4 个按钮,但不超过这 2 列。然而,我得到的是所有 4 个按钮都延伸到 4 列,超过了两列。 cancelButton 也有一个奇怪的水平伸长。
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbl.setConstraints(someComponenetHere, gbc);
gbc.gridy = 2;
gbl.setConstraints(applyButton, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbl.setConstraints(cancelButton, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbl.setConstraints(applyAddButton, gbc);
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbl.setConstraints(applyAddAllButton, gbc);
so in my japanel, I have a componenet which is 2 columns wide. I would like to add 4 buttons at the bottom not exceeding this 2 column. however, what I get is all 4 buttons stretch out across to 4 columns, exceeding two clumns. the cancelButton also has a weird horizontal elongation .
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbl.setConstraints(someComponenetHere, gbc);
gbc.gridy = 2;
gbl.setConstraints(applyButton, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbl.setConstraints(cancelButton, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbl.setConstraints(applyAddButton, gbc);
gbc.gridx = 3;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbl.setConstraints(applyAddAllButton, gbc);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的方法是让你组成 4 列宽,并在每列中粘贴一个按钮。
如果失败,您可以创建一个包含 2 个按钮的面板,并将其中一个面板粘贴到每一列中。
My approach would be to make you component 4 columns wide and stick a button in each column.
Failing that you could create a Panel containg 2 buttons and stick one of those panels in each column.