Java 中属性页的布局

发布于 2024-12-11 03:33:32 字数 1580 浏览 0 评论 0原文

是否有一种简单的方法可以以与属性页类似的方式在面板中布局组件?其中每个属性或设置在面板左侧都有一个标签,在右侧有一个编辑器组件。理想情况下,标签应左对齐,编辑器右对齐。编辑器左侧位置应从最大标签宽度加上间隙开始。所有编辑器应该具有相同的宽度。

目前,我只能使用 Netbeans 设计器使用“自由设计”来执行此操作,该设计器对应于生成的代码中的组布局。即使如此,我仍然使用设计器来设置位置和大小。公平地说,生成的代码实在是太丑了。

生成的代码

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(10, 10, 10)
            .addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(199, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(11, 11, 11)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(275, 275, 275))
    );

现在请记住,我想在代码中执行此操作 - 而不是在 Netbeans 设计器中,而且我还想添加许多组件。

我不认为具有 2 列和 x 行的网格布局会像往常一样在面板中间分成 2 列。因此,当所有标签都不是太大时,它与编辑器之间就会出现巨大的差距。

欢迎任何提示、技巧、窍门。我正在考虑编写自己的布局管理器来实现这一点。

不过,编写一个并在 Netbeans 设计器中使用它会很好。但这有点操之过急。

Is there a simple way to layout components in a panel in a similar fashion to a Property Page? Where each property or setting has a label to the left and an editor component to the right of the panel. Ideally, the labels should be left aligned, and the editors right aligned. The editor left position should start from the largest label width plus a gap. All editors should possibly have the same width.

At present, I can only do this using Netbeans designer using the "Free design" which corresponds to a Group layout in the generated code. Even then I am using the designer to set location and size. And to be fair the generated code is god darn ugly.

Generated Code

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(10, 10, 10)
            .addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(199, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(11, 11, 11)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(275, 275, 275))
    );

Now bear in mind, I want to do this in code - not in Netbeans designer, and I also want to add many components.

I don't think Grid Layout with 2 columns and x rows will cut it as always splits into 2 columns in the middle of the panel. Therefore where all labels are not too big, a huge gap will appear between itself and the editor.

Any tips, tricks, hacks are welcome. I am thinking about writing my own layout manager to possibly do this.

Although it would be nice to write one and use it in the Netbeans designer. But that is jumping the gun a little.

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

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

发布评论

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

评论(1

伴梦长久 2024-12-18 03:33:32

您想要对组件的外观设置的要求越多,为了实现这一目标,您需要编写的代码就越复杂。

如果 GridLayout 的行为不符合您的要求,那么 GridBagLayout 也会。但我担心,当您实现 GridBagLayout 时,您的代码看起来不会比这里作为示例的代码好多少。

我会坚持使用现有布局并忽略 Netbeans 设计器生成的“丑陋”代码。无论如何,您不必搞乱它,因为您以图形方式完成所有编辑。

The more requirements you want to set for the looks of your components, the more complicated the code you will need to write, in order to get there, will be.

If GridLayout does not behave the way you like, then GridBagLayout will. I am afraid though, that when you implement a GridBagLayout, your code will not look much nicer than what you've got here as an example.

I would stick with the existing layouts and ignore the "ugly" code generated by Netbeans designer. You do not have to mess with it anyway, since you do all the editing graphically.

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