Java Swing:选择正确的 LayoutManager
我正在构建一个 PropertyPanel。目前,我正在使用 GridLayout 来管理 JLabels 及其相应的字段,我可以在其中指定值。但问题是 GridLayout 自动管理列的大小:它使它们具有相同的宽度。
这意味着当我有一个大值字段时,该列会变得更大(这很好),但另一列(包含我的所有 JLabel
)也会变得更大。这是屏幕截图:
<强>< BAD
如您所见,image
属性具有巨大的值,这使得两列都变大,并且在 JLabel
之后有很多空间s。
因此,我正在寻找一个 LayoutManager
,它可以使每列尽可能大。
我想要一个像这样的布局(它是用 Gimp 编辑的):
<强><好
谢谢
I'm building a PropertyPanel. Currently I'm using a GridLayout
to manage the JLabels
and their corresponding fields where I can specify the value. But the problem is that the GridLayout
automatically manages the size of the columns: it makes them the same width.
This means when I'm having a big value field, the colum, is getting bigger (which is good), but the other column (with all my JLabel
s) is getting bigger as well. Here is a screenshot:
< BAD
As you can see, the image
property has a huge value, which makes both columns bigger, and I'm having a lot of space after the JLabel
s.
So, I'm searching for a LayoutManager
which makes each column as big as necessary.
I want a layout like this (it's edited with Gimp):
< GOOD
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 SpringLayout 来实现此目的。请参阅如何使用 SpringLayout。
布局示例:
请记住,您还可以嵌套布局。
You can use SpringLayout for this. See How to Use SpringLayout.
Example layout:
Remember that you also can nest layouts.
SpringLayout
是我通常用于此类表单的布局。尽管我认为 GridBagLayout 也能很好地工作。SpringLayout
is what I typically use for forms like this. Although I thinkGridBagLayout
would also work nicely.我倾向于尝试通过混合 GridLayout 和 BorderLayout 来破解所有内容,所以也许这不是最好的解决方案,但是......
创建两个 GridLayout,它们都有一个列。一个用于标签,另一个用于控件。
现在创建一个 BorderLayout 作为父级。
将左侧网格添加到 BorderLayout.WEST,将右侧网格添加到 BorderLayout.CENTER。
I tend to try to hack everything by mixing GridLayout and BorderLayout, so maybe it's not the best solution but...
Create two GridLayouts, both have a single column. One for the labels the other for the controls.
Now create a BorderLayout to be the parent.
Add the left grid to the BorderLayout.WEST and the right grid to the BorderLayout.CENTER.
虽然这个问题已经在 11 小时前得到了答复,但我只是想顺便过来一下。提出建议。我建议GroupLayout。
我最近想打破名称/值对话框的嵌套布局,并查看了
GroupLayout
和GroupLayout
。 SpringLayout。 SpringLayout 提供的唯一优势似乎是它可以实现标签文本的右对齐(可能有一种方法可以使用 GL 来实现,但我不知道如何实现)。缺点是,SpringLayout
的 Java 教程示例使用了巨大的“帮助类”来定义布局约束。最后(这只是一个很短的“研究”)我选择使用
GroupLayout
。While this was answered 11 hours ago, I just thought I'd pop in & make a suggestion. I suggest GroupLayout.
I was looking to break from nested layouts for a name/value dialog recently and looked at both
GroupLayout
&SpringLayout
. It seemed the only advantage offered bySpringLayout
was that it could achieve the right aligned text of the labels (there may be a way to do it using GL, but I couldn't figure out how). On the downside, the Java Tutorial examples forSpringLayout
used a whopping 'helper class' to define layout constraints.In the end (it was only a very short 'study') I chose to use
GroupLayout
.考虑使用 MigLayout。如果受当前 JDK 的限制,请使用 GridBagLayout。
Consider using MigLayout. If constrained within the current JDK, GridBagLayout.
以下是标准 LayoutManager 的概述:
http://download.oracle.com/javase/tutorial/uiswing/layout例如,
如果您想手动编写 GUI 代码,您可以使用 GridBagLayout 或非标准 MigLayout。
如果您想使用 GUI 构建器(例如 NetBeans 中的构建器),您可以使用 GroupLayout。
Here's an overview of the standard LayoutManagers:
http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
You could e.g. use GridBagLayout or the non-standard MigLayout, if you want to code the GUI by hand.
If you want to use a GUI builder (e.g. the one in NetBeans) you could use the GroupLayout.