Java 选择布局问题

发布于 2025-01-03 17:31:52 字数 418 浏览 0 评论 0原文

大家好,我正在尝试制作一个顶部带有导航栏的屏幕,但不确定哪种布局最适合使用。基本上我希望屏幕看起来像:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2025-01-10 17:31:52

“导航栏”听起来像是它本身也可以是一个组件。如果额外的嵌套级别不是问题,我只需使用 < 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 component NORTH, and the empty panel (where the rest of the UI is I suppose) into the CENTER.

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.)

樱娆 2025-01-10 17:31:52

您不会想在 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 the GridBagConstraints for the navigation panel a weighty of 0 and the empty panel a weighty of 1 (or any positive number). This will cause the empty panel to take up any extra space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文