与 Java Swing 的 GroupLayout 混合对齐
我正在尝试在我的应用程序中构建一个 GUI 窗口。我想做的是有一个窗口,顶部有几个按钮,还有一个大的文本区域。像这样的事情:
+--------------------------------------------------+
| [button1] [button2] [button3] |
| +----------------------------------------------+ |
| | text area | |
| | | |
| | | |
| | | |
| +----------------------------------------------+ |
+--------------------------------------------------+
我几乎就在那里,使用 GroupLayout:
layout.setHorizontalGroup(
layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(button1)
.addComponent(button2))
.addComponent(closeWindow))
.addComponent(textarea1)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(button1)
.addComponent(button2)
.addComponent(button3))
.addComponent(textarea)
);
问题是,这最终导致 Button3 与其他两个对齐到左侧。我似乎无法弄清楚如何仅在那个按钮上指定对齐方式。我可以在整个按钮栏上执行 GroupLayout.Alignment.TRAILING,但这会击中所有 3 个按钮,这也不太正确。
那么正确的做法是什么呢?由于对齐仅适用于并行组,因此我认为包含两个顺序组的 HorizontalGroup 不会有帮助吗?
我缺少什么?
I'm trying to build a GUI window in my application. What I'm trying to do is have a window, with a few buttons at the top, and a large text area. Something like this:
+--------------------------------------------------+
| [button1] [button2] [button3] |
| +----------------------------------------------+ |
| | text area | |
| | | |
| | | |
| | | |
| +----------------------------------------------+ |
+--------------------------------------------------+
I'm almost there, using GroupLayout:
layout.setHorizontalGroup(
layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(button1)
.addComponent(button2))
.addComponent(closeWindow))
.addComponent(textarea1)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(button1)
.addComponent(button2)
.addComponent(button3))
.addComponent(textarea)
);
The problem is that this ends up with button3 aligned to the left, with the other two. I can't seem to figure out how I can specify the alignment on just that one button. I can do GroupLayout.Alignment.TRAILING on the entire button bar, but that hits all 3 buttons, which is also not quite right.
So what's the correct approach? Since the alignment only applies for Parallel Groups, I don't think having a HorizontalGroup with two Sequential Groups in it will help?
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在连续组中添加间隙。保持水平组不变:
与这些参数的间隙充当“弹簧”,占据所有可用空间。
Add a gap in your sequential group. Leaving your horizontal group as is:
The gap with those paramters acts as a "spring", taking up all available space.
尝试
在第二个按钮后面添加:。 MAX_VALUE 将导致间隙根据需要扩大。
Try adding:
after the second button. The MAX_VALUE will cause the gap to expand as much as necessary.
您想要使用仅适用于连续组的 addPreferredGap() 。下面的代码为您提供了所需的布局。
You want to use addPreferredGap() which is only available on sequential groups. The code below gives you the desired layout.