与 Java Swing 的 GroupLayout 混合对齐

发布于 2024-08-29 14:45:54 字数 1325 浏览 7 评论 0原文

我正在尝试在我的应用程序中构建一个 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 个按钮,这也不太正确。

那么正确的做法是什么呢?由于对齐仅适用于并行组,因此我认为包含两个顺序组的 Horizo​​ntalGroup 不会有帮助吗?

我缺少什么?

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 技术交流群。

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

发布评论

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

评论(3

春花秋月 2024-09-05 14:45:54

在连续组中添加间隙。保持水平组不变:

layout.setVerticalGroup(
    layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup()
        .addComponent(button1)
        .addComponent(button2)
        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(button3))
      .addComponent(textarea)
  );

与这些参数的间隙充当“弹簧”,占据所有可用空间。

Add a gap in your sequential group. Leaving your horizontal group as is:

layout.setVerticalGroup(
    layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup()
        .addComponent(button1)
        .addComponent(button2)
        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(button3))
      .addComponent(textarea)
  );

The gap with those paramters acts as a "spring", taking up all available space.

卖梦商人 2024-09-05 14:45:54

尝试

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE)

在第二个按钮后面添加:。 MAX_VALUE 将导致间隙根据需要扩大。

Try adding:

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, Short.MAX_VALUE)

after the second button. The MAX_VALUE will cause the gap to expand as much as necessary.

独自←快乐 2024-09-05 14:45:54

您想要使用仅适用于连续组的 addPreferredGap() 。下面的代码为您提供了所需的布局。

    layout.setHorizontalGroup(
            layout.createParallelGroup()
                    .addGroup( layout.createSequentialGroup()
                            .addComponent( button1 )
                            .addComponent( button2 )
                            .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE )
                            .addComponent( button3 ) )
                    .addComponent( textArea )
    );
    layout.setVerticalGroup(
            layout.createSequentialGroup()
                    .addGroup( layout.createParallelGroup()
                            .addComponent( button1 )
                            .addComponent( button2 )
                            .addComponent( button3 ) )
                    .addComponent( textArea )
    );

You want to use addPreferredGap() which is only available on sequential groups. The code below gives you the desired layout.

    layout.setHorizontalGroup(
            layout.createParallelGroup()
                    .addGroup( layout.createSequentialGroup()
                            .addComponent( button1 )
                            .addComponent( button2 )
                            .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE )
                            .addComponent( button3 ) )
                    .addComponent( textArea )
    );
    layout.setVerticalGroup(
            layout.createSequentialGroup()
                    .addGroup( layout.createParallelGroup()
                            .addComponent( button1 )
                            .addComponent( button2 )
                            .addComponent( button3 ) )
                    .addComponent( textArea )
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文