阻止 GroupLayout 组件垂直拉伸

发布于 2024-09-16 18:25:41 字数 1003 浏览 17 评论 0原文

有没有一种简单的方法可以让 GroupLayout 应用程序中的所有(或大部分)组件不垂直拉伸?我知道我可以通过在添加时强制每个组件达到其首选大小来做到这一点,但这会使代码变得更加冗长:

       .addGroup(layout.createSequentialGroup()
          .addComponent(oDevRadio)
          .addComponent(oInstRadio)
       )

Becomes

       .addGroup(layout.createSequentialGroup()
          .addComponent(oDevRadio,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
          .addComponent(oInstRadio,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
       )

有没有办法将其设置为默认值,并且只需指定元素我 想要可拉伸吗?

参考文献 - addComponent 的规范

Is there an easy way to get all (or most) of the components in a GroupLayout application NOT stretch vertically? I know I can do it by forcing each component to it's preferred size when I add it, but that makes the code so much more verbose:

       .addGroup(layout.createSequentialGroup()
          .addComponent(oDevRadio)
          .addComponent(oInstRadio)
       )

Becomes

       .addGroup(layout.createSequentialGroup()
          .addComponent(oDevRadio,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
          .addComponent(oInstRadio,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
       )

Is there a way to set it as a default, and just specify the elements I want to be stretchable?

References
- addComponent's spec

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

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

发布评论

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

评论(2

迟到的我 2024-09-23 18:25:42

据我所知还没有。我用一个实用程序类来处理它:

package alpha;

import java.awt.Component;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Group;

public class GroupLayoutUtil
{
    public static GroupLayout.Group addPreferred(Group g, Component c)
    {
        return g.addComponent(c, GroupLayout.PREFERRED_SIZE, 
                GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
    }
}

Not as far as I know. I've handled it with a utility class:

package alpha;

import java.awt.Component;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Group;

public class GroupLayoutUtil
{
    public static GroupLayout.Group addPreferred(Group g, Component c)
    {
        return g.addComponent(c, GroupLayout.PREFERRED_SIZE, 
                GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
    }
}
一场信仰旅途 2024-09-23 18:25:41

据我所知,告诉 GroupLayout 组件不要拉伸或错位的唯一方法要求相关组件位于 ParallelGroup 内。将 ParallelGroup 的可调整大小标志设置为 false 就很简单了。

具有相关标志的 ParallelGroup 创建者的 Javadoc

例如,在下面的代码中 jspCasts 是一个非常高的组件。如果没有将标志设置为 false 的新 ParallelGroup,则其旁边的组件要么会拉伸,要么不会按照应有的方式整齐对齐。

vGroup.addGroup(gl.createParallelGroup(Alignment.LEADING).
    addComponent(jspCasts).
    addGroup(gl.createParallelGroup(Alignment.CENTER, false).
      // without worrying about vertical stretching or misalignment, 
      // add your components here

As far as I know, the only method of telling GroupLayout components not to stretch or otherwise be misaligned requires that the relevant components be inside a ParallelGroup. It is then a simple matter to set the resizeable flag of the ParallelGroup to false.

Javadoc of ParallelGroup creator with relevant flag

For example, in the following code jspCasts is a very tall component. Without a new ParallelGroup with the flag being set to false, components next to it would either stretch or not align neatly as they should.

vGroup.addGroup(gl.createParallelGroup(Alignment.LEADING).
    addComponent(jspCasts).
    addGroup(gl.createParallelGroup(Alignment.CENTER, false).
      // without worrying about vertical stretching or misalignment, 
      // add your components here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文