Java GridBag 布局
我在使用 GridBagLayout 时遇到问题,我似乎无法掌握它。 下面是我的 GUI 应该是什么样子的,我似乎无法让它工作。 主要问题是排列文本框和文本框。标签在右侧和左手边。
I am having a problem using GridBagLayout, I cant seem to get a hang of it.
below is what my GUI should look like, I cant seem to get it working.
The main problem is lining the text boxes & labels at the right & left hand side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将有一个使用 BorderLayout 的主 JPanel。
然后,我将有两个从属 JPanel,一个使用 BorderLayout.WEST 添加到主 JPanel,另一个使用 BorderLayout.EAST 添加到主 JPanel。
西边的 JPanel 将使用 GridBagLayout,4 列和 4 行。我会使用 Insets 来获得我想要的间距。
东JPanel将使用GridBagLayout,4列6行。再次,我将使用 Insets 来获得我想要的间距。
我会将按钮放在使用 FlowLayout 的 JPanel 中。我设置了 JPanel 的首选大小,以便按钮保持在同一行。然后我将按钮 JPanel 添加到东 JPanel 作为第 6 行。
I would have a main JPanel that uses the BorderLayout.
Then I would have two subordinate JPanels, one added to the main JPanel using BorderLayout.WEST, and the other added to the main JPanel using BorderLayout.EAST.
The west JPanel would use the GridBagLayout, 4 columns and 4 rows. I would use Insets to get the spacing that I want.
The east JPanel would use the GridBagLayout, 4 columns and 6 rows. Again, I would use Insets to get the spacing that I want.
I would put the buttons inside of a JPanel that uses FlowLayout. I'd set the preferred size of the JPanel so the buttons stay on the same row. Then I'd add the button JPanel to the east JPanel as the 6th row.
如果您使用的是 Eclipse,我建议使用 Window Builder Pro 将 UI 置于您想要的位置。您将能够看到可以使用 Gridbag 布局转动的所有“转盘”。
您可能需要尝试一下事物的锚定方式,以使标签按照您想要的方式排列。
If you are using eclipse, I'd suggest using Window Builder Pro to get the UI where you you want it. You'll be able to see all the 'dials' you can turn with Gridbag layout.
You'll probably need to play with how things are anchored to get the labels to line up how you want.
可能是嵌套布局。对于左上角和右侧的“控件组”,将它们全部交换为
JTable
1 每个或使用GroupLayout
2。1) JTable
2) GroupLayout
另请参阅
Probably a nested layout. For the 'groups of controls' on the top left and the right hand side, swap them all for a
JTable
1 each or useGroupLayout
2.1) JTable
2) GroupLayout
See also