微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 GridBagLayout 单元格内收缩 JLabel

如何解决在 GridBagLayout 单元格内收缩 JLabel

问题

我有一个 JPanelGridBagLayout 作为布局管理器。我希望布局的列具有一定的宽度。有时,列的宽度可能低于内部组件的首选宽度。在这种情况下,列应强制组件仅占用列的可用空间。 但是,就目前而言,如果列宽约束小于组件的首选宽度,单元格内的组件不会收缩。

示例

以下是 4x4 网格的演示。我希望左上角JLabel 按照我在初始化时设置的最小宽度缩小 GridBagLayout (10 px)

当我运行下面的例子时,得到以下结果:

https://i.stack.imgur.com/DM4LE.png

左上角的单元格占用的空间比 GridBagLayout 最初允许的多

public class ResizeDemo extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokelater(() -> {
            ResizeDemo resizeDemo = new ResizeDemo();
            resizeDemo.pack();
            resizeDemo.setDefaultCloSEOperation(EXIT_ON_CLOSE);
            resizeDemo.setVisible(true);
        });
    }

    public ResizeDemo() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0,0};
        gridBagLayout.rowHeights = new int[]{0,0};
        gridBagLayout.columnWeights = new double[]{1.0,Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{1.0,Double.MIN_VALUE};
        getContentPane().setLayout(gridBagLayout);

        JPanel panel = new JPanel();
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        getContentPane().add(panel,gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        
        
        // ============  Set the column widths here ============
        //  Note that the width of the first column is 10 px 
        gbl_panel.columnWidths = new int[] {10,150,0};
        // =====================================================
        
        gbl_panel.rowHeights = new int[]{0,0};
        gbl_panel.columnWeights = new double[]{0.0,0.0,0.0};
        gbl_panel.rowWeights = new double[]{0.0,Double.MIN_VALUE};
        panel.setLayout(gbl_panel);

        JLabel label1 = new JLabel("A very long string here");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        GridBagConstraints gbc_label1 = new GridBagConstraints();
        gbc_label1.fill = GridBagConstraints.HORIZONTAL;
        gbc_label1.gridx = 0;
        gbc_label1.gridy = 0;
        panel.add(label1,gbc_label1);

        JLabel label2 = new JLabel("Label");
        GridBagConstraints gbc_label2 = new GridBagConstraints();
        gbc_label2.gridx = 1;
        gbc_label2.gridy = 0;
        panel.add(label2,gbc_label2);

        JLabel label3 = new JLabel("Label");
        GridBagConstraints gbc_label3 = new GridBagConstraints();
        gbc_label3.fill = GridBagConstraints.HORIZONTAL;
        gbc_label3.gridx = 0;
        gbc_label3.gridy = 1;
        panel.add(label3,gbc_label3);

        JLabel label4 = new JLabel("Label");
        GridBagConstraints gbc_label4 = new GridBagConstraints();
        gbc_label4.gridx = 1;
        gbc_label4.gridy = 1;
        panel.add(label4,gbc_label4);

    }
}

问题:

如何强制 JLabel 单元格内的 GridBagLayout 自行缩小?

解决方法

GridBagLayout 根据添加到列的任何组件的最大“首选大小”来调整每列的大小。

“columnWidths”用于设置任何列的最小值。这在您调整框架大小时使用。如果没有足够的空间以首选大小显示组件,并且您为该组件设置了调整大小权重,则该组件的大小将从其首选大小缩小到其最小大小。

如何强制 GridBagLayout 单元格内的 JLabel 自行缩小?

一种可能性是使用 BoxLayout 将标签包裹在面板中。 BoxLayout 将遵守组件大小的最大大小。基本逻辑是:

//panel.add(label1,gbc_label1);
Dimension max = label1.getPreferredSize();
max.width = 10;
label1.setMaximumSize(max);
Box wrapper = Box.createHorizontalBox();
wrapper.add(label1);
panel.add(wrapper,gbc_label1);

不知道您真正的应用程序是什么,但如果您只使用标签,那么也许您可以使用 JTable。使用 JTable,您可以控制实际的列宽。

,

这是您的代码中设置宽度的部分。缺少设置列宽默认缩小以适应封闭的组件。

gbl_panel.columnWidths = new int[] {10,150,0};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。