布局管理器可以生成多个 JPanel 吗?

发布于 2024-07-13 02:03:03 字数 595 浏览 5 评论 0原文

我必须构建一个带有许多控件的相当大的表单。 这些控件分为基本控件/设置和扩展控件/设置。 用户可以决定是否只想查看基本控件或同时查看基本控件和扩展控件。

我已将所有扩展控件放到它们自己的 JPanel 上,以便我可以通过显示或隐藏此面板轻松地在两个视图之间切换。

目前我正在使用 GroupLayout,发生的情况是不同面板上的控件未对齐:

Label aaa:     Text field
Label a:       Text field
Label aaaaaa:  Text field
----------------------------
Label b:    Text field
Label bbb:  Text field
Label bb:   Text field

不幸的是,我现在找到了“同步”两个面板布局的方法(除了使用 AbsoluteLayout 和固定控件坐标)

  • 有没有办法实现这?
  • 我的整个设计有缺陷吗?

编辑:如果可能的话我想保留 GroupLayout 管理器。

I have to build a rather large form with many controls. The controls are divided in basic controls/settings and extended controls/settings. The user can decide if he wants to see only the basic or both basic and extended controls.

I've dropped all extended controls onto their own JPanel so that I can easily switch between the two views by showing or hiding this panel.

Currently I'm using GroupLayout and what happens is that the controls on different panels are not aligned:

Label aaa:     Text field
Label a:       Text field
Label aaaaaa:  Text field
----------------------------
Label b:    Text field
Label bbb:  Text field
Label bb:   Text field

Unfortunatly I found now way to "synchronize" the layouts of the two panels (except using AbsoluteLayout and fixed control coordinates)

  • Is there any way to achive this?
  • Is my whole design flawed?

EDIT: If it is possible I would like to keep the GroupLayout manager.

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

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

发布评论

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

评论(6

几味少女 2024-07-20 02:03:03

据我所知,没有 Swing LayoutManager(来自 JRE 或开源)可以跨越多个面板。

我目前正在为我的 DesignGridLayout 项目开发这样的功能(我称之为“布局同步”) ,但这不是一件容易实现的事情(我大约两周前就开始了,我仍然不知道是否以及何时我会得到一些有趣的东西,但我仍然对它抱有很高的希望;-))

你的一个选择可以检查的是将所有组件添加到同一面板(然后只有一个 GroupLayout)并根据用户的选择隐藏/显示它们。 希望 GroupLayout 能够根据情况调整大小(在调用 pack() 之后)。

如果 GroupLayout 表现良好,那么每次用户更改其选择以显示/隐藏扩展字段后,只需调用 pack() 即可。

否则,每次用户更改其选择时,您都必须手动设置面板的大小。

As far as I know, no Swing LayoutManager (from JRE or open source) can span several panels.

I am currently working on such a feature (which I called "layouts synchronization") for my DesignGridLayout project, but it is not something easy to implements (I have started about 2 weeks ago and I still don't see exactly if and when I will get to something interesting, but I still have high hope for it;-))

One option you could check would be to add all components to the same panel (with just one GroupLayout then) and hide/show them based on user's selection. Hopefully, GroupLayout will adapt the size to the situation (after calling pack()).

If GroupLayout behaves well, then it would just be a matter of calling pack() each time after user changes his selection to show/hide extended fields.

Else you would have to manually set the size of your panel every time the user changes his selection.

淡水深流 2024-07-20 02:03:03

也许最简单(好的)方法是将所有组件添加到主面板。 将子面板设置为不透明,并将也添加到主面板。 主面板需要关闭优化绘图。

另一种技术是添加间隔组件。 在底部面板中添加一个与标签同一列的组件,该组件动态地从顶部标签获取其各种尺寸方法的宽度组件。 对顶部面板进行相反的操作。

Probably the easiest (good) way to do it is to add all the components to the main panel. Set the subpanels to non-opaque, and add the also to the main panel. The main panel the needs optimised drawing to be switched off.

Another technique is to add a spacer component. To the bottom panel add a component in the same column as the labels which dynamically takes the width component of its various size methods from the top labels. Do the same in reverse to the top panel.

神妖 2024-07-20 02:03:03

我认为标准布局管理器无法做到这一点。 您可能必须编写自己的布局管理器,但如果您继承 GroupLayout 的话,这应该不会太难。

I think there is no way to do it with the standard layout managers. You'll probably have to write your own layout manager, but it shouldn't be too hard if you subclass GroupLayout.

一页 2024-07-20 02:03:03

您可以使用 GridLayout 而不是 GroupLayout,这将为您提供统一的列间距

You could use GridLayout instead of GroupLayout which will give you uniform spacing between the columns

安静被遗忘 2024-07-20 02:03:03

如果您想将它们保留在具有单独布局的单独面板中:

迭代您添加的所有标签,并找到每个标签的最大首选宽度。
第二次迭代,并将首选大小设置为每个标签的首选高度,但最大宽度。

If you want to keep them in separate panels with separate layouts:

Iterate over all of the labels that you add, and find the maximum preferred width of each.
Iterate a second time, and set the preferred size to that each label's preferred height, but the maximum width.

落日海湾 2024-07-20 02:03:03

这是GridLayout的解释。 这会将每个组件设置为您期望的大小。 使用 GridData 对象,您可以指定组件的排序方式。

示例

示例 net/wUbVo.png" rel="nofollow noreferrer">alt text
(来源:sun.com)

This is the explanation of th GridLayout. This will set every component to the size, you expect it. With the GridData object you can specify how the components are ordere.

Examples

alt text
(source: sun.com)

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