Java Swing:选择正确的 LayoutManager

发布于 2024-11-16 15:30:38 字数 583 浏览 2 评论 0原文

我正在构建一个 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 JLabels) is getting bigger as well. Here is a screenshot:

enter image description here     < 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 JLabels.

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

enter image description here     < GOOD

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

过度放纵 2024-11-23 15:30:38

您可以使用 SpringLayout 来实现此目的。请参阅如何使用 SpringLayout

布局示例:

在此处输入图像描述

请记住,您还可以嵌套布局。

You can use SpringLayout for this. See How to Use SpringLayout.

Example layout:

enter image description here

Remember that you also can nest layouts.

眼眸印温柔 2024-11-23 15:30:38

SpringLayout 是我通常用于此类表单的布局。尽管我认为 GridBagLayout 也能很好地工作。

SpringLayout is what I typically use for forms like this. Although I think GridBagLayout would also work nicely.

绅刃 2024-11-23 15:30:38

我倾向于尝试通过混合 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.

慈悲佛祖 2024-11-23 15:30:38

虽然这个问题已经在 11 小时前得到了答复,但我只是想顺便过来一下。提出建议。我建议GroupLayout

我最近想打破名称/值对话框的嵌套布局,并查看了 GroupLayoutGroupLayout 。 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 by SpringLayout 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 for SpringLayout used a whopping 'helper class' to define layout constraints.

In the end (it was only a very short 'study') I chose to use GroupLayout.

匿名。 2024-11-23 15:30:38

考虑使用 MigLayout。如果受当前 JDK 的限制,请使用 GridBagLayout

Consider using MigLayout. If constrained within the current JDK, GridBagLayout.

心的憧憬 2024-11-23 15:30:38

以下是标准 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.

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