Java Swing:FlowLayout JPanel 彼此相邻?
我在 JFrame 上绘制了三个 JPanel。目前这些都设置为使用默认的 FlowLayout。我希望这些在一个列中一个在另一个之上。
然而,我发现它们在同一条线上彼此相邻浮动,只要它们内部的组件一致。
FlowLayout JPanel 的自然宽度是其内容的总和吗?如果是这样,有什么方法可以强制该区域的宽度为 JFrame 的宽度吗?
有趣的是,我发现,如果“顶部”和“底部”面板的内容跨越 JFrame 的整个宽度,并且“中间”面板留空,则“中间”面板会在两个面板之间创建一个空间,很像旧的 html 的“
”。
谢谢,
Ben
I have three JPanels painted onto a JFrame. These are currently all set to use the default FlowLayout. I would like for these to be one above the other in a single column.
However, I am finding that these are floating next to each other on the same line, so long as the components within them.
Is the natural width of a FlowLayout JPanel the sum of its contents? If so, is there any way to force the width of the region to be the width of the JFrame?
Interestingly, I am finding that, if the "top" and "bottom" panels have content which spans the entire width of the JFrame, and the "middle" panel is left empty, that the "middle" panel creates a space between the two, much like the old "
of html.
Thanks,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Jim 所说,如果您需要线性对齐组件,BoxLayout 是正确的选择。
这是一个示例:
您最好仔细阅读 布局管理器简介,了解 LayoutManager 的基本集。当需要进行复杂布局时,请使用 MigLayout 而不是尝试学习 GridBagLayout - 你会感谢我。
As Jim stated, BoxLayout is the correct choice if you need to linearly align components.
Here is an example:
You would do well to really read through the introduction to layout managers to understand the base set of LayoutManagers. When it comes time to do complex layouts, please use MigLayout instead of trying to learn the GridBagLayout - you will thank me.
如果您希望创建垂直布局,您可能需要考虑使用 BoxLayout 作为封闭容器。可以将其设置为沿 y 轴布局组件。
If you wish to create a vertical layout you may want to look into using BoxLayout for the enclosing container. This can be set to layout components along the y axis.
您可以使用
GridLayout
在组件中创建YxZ
网格。要更精确地构建布局,您可以使用 GridBagLayout ,它提供了对嵌套组件的定位和大小调整的完全控制。You can use
GridLayout
to createYxZ
grid in a component. To construct layout more precisely you can useGridBagLayout
which provides the full control of positioning and sizing for nested components.我总是使用 NetBeans。因为,我可以进行自由设计,而不会在将组件放置在 JPanel 或 JFrame 上时遇到任何麻烦:) 您可以考虑一下。
I am always go with NetBeans. Since, I can make Free Design with out having any trouble of placing the components on the JPanel or JFrame :) You may consider it.