首页 > 解决方案 > 当仅使用一个单元格时,如何在 Miglayout 中强制每行 4 个单元格等宽?

问题描述

我有一个动态宽度的面板。添加到面板的组件应从左到右排列,每行最多 4 个组件(单元),其中每个组件/单元填充面板宽度的 25%。如果只有一个组件,它应该填充父母宽度的 25% - 空间的剩余 75%(3 个单元格)应该是空的。

我使用 hack 让它工作,但我对该实现不满意 - 使用单元格并为每个未使用的单元格创建一个“空”面板。请参阅下面的代码片段:

MigLayout migLayout = new MigLayout("fillx, insets 0, wrap 4", "", "");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel("1");
JLabel label2 = new JLabel("2");
JPanel filler1 = new JPanel();
JPanel filler2 = new JPanel();
panel.add(label1, "cell 0 0);
panel.add(label2, "cell 1 0);
panel.add(filler1, "cell 2 0);
panel.add(filler2 , "cell 3 0);

这给出了如下图所示的内容,其中外括号是外面板,两个内括号是标签:

[  [ 1 ]  [ 2 ]                ]

而不是使用填充物,我希望它可以设置约束,如下所示:

MigLayout migLayout = new MigLayout("always 4 cells, fillx", "[fill][fill][fill][fill]", "");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
panel.add(label1);
panel.add(label2);

我尝试了各种布局约束并添加了组件参数,但它们的格式通常是这样的:

[  [ 1 ]         [ 2 ]         ]

标签: javaswinglayout-managermiglayout

解决方案


我会选择GridLayout。GridLayout 将根据给定的行/列将您的面板分成单元格(检查其构造函数)。然后每个组件将 100%(宽度和高度)适合单元格。

但是:如果您将网格布局设置为 1 行 4 列(您的情况)并且仅添加 1 个组件,则布局将调整为:1 行和 1 列,因为它不会留出空白空间。

技巧/解决方案:添加一个空组件的方式与我们向 BoxLayout 添加间隙的方式完全相同。

private static Component createSpace() {
    return Box.createRigidArea(new Dimension(1, 1));
}

缺点: 如果您想在面板显示后将组件添加到面板中,则必须删除空格,因此您必须将所有空组件存储到结构中或执行以下操作(我总是喜欢它):

gridLayoutPanel.removeAll();
gridLayoutPanel.add(component1);
gridLayoutPanel.add(newComponent); //This was space before
gridLayoutPanel.add(createSpace());
gridLayoutPanel.add(createSpace());
gridLayoutPanel.repaint();
gridLayoutPanel.revalidate();

SSCCE将是(忽略组件的高度):

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GridLayoutTest extends JFrame {
    public GridLayoutTest() {
        getContentPane().setLayout(new GridLayout(1, 4));
        JButton component1 = new JButton("Component1");
        JButton component2 = new JButton("Component2");
        JButton component3 = new JButton("Component3");
        add(component1); //25% of the width
        add(component2); //25% of the width
        add(component3); //25% of the width
        add(createSpace()); //25% of the width
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 200);
        setLocationRelativeTo(null);
    }

    private static Component createSpace() {
        return Box.createRigidArea(new Dimension(1, 1));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new GridLayoutTest().setVisible(true));
    }
}

推荐阅读