Swing GroupLayout:调整和限制组件大小 [英] Swing GroupLayout: Resizing and limiting component sizes

查看:36
本文介绍了Swing GroupLayout:调整和限制组件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GroupLayout 来管理一些动态生成的数据输入表单中的组件.布局或多或少是这样的:

I'm using GroupLayout to manage components in some dynamically generated data input forms. The layout is more or less like so:

*-----------------------------------------------*
| label A     |   field A                       | 
| label B     |   field B                       |
| label C     |   field C                       |
*-----------------------------------------------*

我在水平布局中使用了 2 个平行组,在垂直布局中使用了一个连续组.大多数情况下,一切正常.

I'm using 2 parallel groups for the horizontal layout, and a single sequential group for the vertical layout. For the most part, everything is working just fine.

我想将标签的最大宽度(它们只是 JLabel 的实例)限制为父 JFrame 宽度的 1/3.如果 JFrame 是固定大小,这将是微不足道的,但我必须处理它被调整大小.

I want to limit the maximum width of the labels (which are just instances of JLabel) to 1/3 of the width of the parent JFrame. If the JFrame was a fixed size this would be trivial, but I have to deal with it being resized.

我正在从 JFrame 中获取 ComponentListener.componentResized() 事件,但是我在收到这样的事件后有点不知所措.

I'm picking up ComponentListener.componentResized() events from the JFrame but I'm a bit stuck on what to do once I receive such an event.

我尝试过这种方法但没有任何运气:

I've tried this approach without any luck:

public void componentResized(ComponentEvent e) {
    int maxW = parentFrame.getWidth() / 3;
    for (JLabel l : labels) {
        l.setMaximumSize( // have also tried setSize() and setPreferredSize()
            new Dimension(
                Math.min(l.getSize().width, maxW),
                l.getMaximumSize().height));
    }
    groupLayout.invalidateLayout(getContentSpace());
}

任何人都可以提出一种限制标签宽度的方法吗?

我可能每次都可以从头开始重新构建布局,但我觉得应该有更简单的方法.

I could probably rebuild the layout from scratch every time, but I feel like there ought to be a simpler way.

推荐答案

在这个例子中,GroupLayout 可以简单地依赖标签的首选大小,而不必求助于任何 setXXXSize() 方法.在这种方法中,

In this example, GroupLayout can simply rely on the preferred size of the label, without having to resort to any setXXXSize() method. In this approach,

  • 标签空间总是容纳最大的标签.
  • 文本字段可以以一种有用的方式调整大小.
  • 跨平台和字体的结果都是正确的.
  • 您无需为大多数组件指定任何内容……因为组件本身默认具有所需的调整大小行为."—如何使用 GroupLayout:组件大小和可调整大小

使用 GroupLayout.Alignment.TRAILING 右对齐标签是个人偏好,我添加了第二个面板来展示它如何嵌套在另一个布局中.

The use of GroupLayout.Alignment.TRAILING to right justify the labels is a personal preference, and I've added a second panel to show how it works nested in another layout.

import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/questions/8492065 */
public class GroupPanel extends JPanel {

    private JLabel label1 = new JLabel("Primary:");
    private JTextField field1 = new JTextField(16);
    private JLabel label2 = new JLabel("Secondary:");
    private JTextField field2 = new JTextField(16);
    private JLabel label3 = new JLabel("Tertiary:");
    private JTextField field3 = new JTextField(16);

    public GroupPanel(int n) {
        this.setBorder(BorderFactory.createTitledBorder("Panel " + n));
        GroupLayout layout = new GroupLayout(this);          
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                .addComponent(label1)
                .addComponent(label2)
                .addComponent(label3))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(field1)
                .addComponent(field2)
                .addComponent(field3))
        );
        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label1)
                .addComponent(field1))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label2)
                .addComponent(field2))
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label3)
                .addComponent(field3))
        );
    }

    private static void display() {
        JFrame f = new JFrame("GroupPanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
        f.add(new GroupPanel(1));
        f.add(new GroupPanel(2));
        f.add(Box.createVerticalGlue());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}

这篇关于Swing GroupLayout:调整和限制组件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆