Java 选择布局问题
大家好,我正在尝试制作一个顶部带有导航栏的屏幕,但不确定哪种布局最适合使用。基本上我希望屏幕看起来像:
[Nav Panel Up top, One row]
[ ^ ]
[ | ]
[ Empty Panel ]
[ | ]
[ | ]
[ | ]
[ ^ ]
嵌套面板包含导航按钮。
我的问题是,我应该使用 GridBagLayout 并将其设置为大约 10 行,然后让空面板展开其中的 9 行,还是 Java 中有更好的布局可以让我做到这一点?
Hey guys I'm trying to make a screen with a navigation bar at the top and am unsure what layout would be the best to use. Basically I want the screen to look like:
[Nav Panel Up top, One row]
[ ^ ]
[ | ]
[ Empty Panel ]
[ | ]
[ | ]
[ | ]
[ ^ ]
The nested panel contains the navigation buttons.
My question is, should I use a GridBagLayout and just set it to having like 10 rows and have the empty panel expand 9 of them or is there a better layout in Java that would allow me to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“导航栏”听起来像是它本身也可以是一个组件。如果额外的嵌套级别不是问题,我只需使用 < code>BorderLayout,放置导航栏组件
NORTH
和空面板 (我想 UI 的其余部分在CENTER
。我更喜欢使用 GridBagLayout 来制作复杂的控件,这些控件无法再拆分为子面板,并且需要有很多边缘需要对齐的布局 - 对于这种情况,它的灵活性有点过大了。 (当然,它仍然非常适合这项任务,只是不是完成它的最简单方法。)
The "navigation bar" sounds like it could as well be a component on its own. If the extra level of nesting isn't a problem, I'd just use
BorderLayout
, put the nav bar componentNORTH
, and the empty panel (where the rest of the UI is I suppose) into theCENTER
.I prefer
GridBagLayout
for making complex controls that can't be split into subpanels anymore, and need layouts where there are a lot of edges to align – its flexibility is a little overkill for this case. (Of course it's still perfectly suitable for this task, just not the simplest way to accomplish it.)您不会想在 GridBagLayout 中使用 9 行来容纳一个组件;它比那更强大。相反,为导航面板的 GridBagConstraints 提供一个
权重
为0
,空面板的权重
为1
(或任何积极的 数字)。这将导致空面板占用任何额外空间。You wouldn't want to use 9 rows in
GridBagLayout
to hold one component; it is more powerful than that. Instead give theGridBagConstraints
for the navigation panel aweighty
of0
and the empty panel aweighty
of1
(or any positive number). This will cause the empty panel to take up any extra space.