有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

GridBagLayout单元格中的java收缩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, 0};
        gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0};
        gbl_panel.rowWeights = new double[]{0.0, 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收缩自身


共 (2) 个答案

  1. # 1 楼答案

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

    gbl_panel.columnWidths = new int[] {10, 150, 0};
    
  2. # 2 楼答案

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

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

    How can I force the JLabel inside a GridBagLayout cell to shrink itself?

    一种可能性是使用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可以控制实际的列宽