为什么我需要在MigLayout中使用“X:X”来将组件集中在多个面板上?

时间:2016-06-07 14:48:25

标签: java miglayout

我尝试使用MigLayout(see the example here)将一些组件集中在几个面板上。 当我将页面分成所有面板中相同的百分比时,它突然起作用了。

但我必须在每个grow前面使用参数“0:0”,我不明白究竟发生了什么。

我转到MigLayout CheatSheet-PageQuickStart-guide,但只能查找值为> 0的参数

  

格式为“min:preferred:max”,但是版本较短,因为例如很少需要指定最大尺寸。   单个值(例如“10”)仅设置首选大小,与“null:10:null”和“:10:”和“n:10:n”完全相同。

     

两个值(例如“10:20”)表示最小和首选大小,与“10:20:null”和“10:20:”和“10:20:n”完全相同< /强>

但它没有解释当你使用0作为参数时会发生什么,或者为什么你需要使用X:X or X:Y而不是例如X:Y:Z or X:X:X

那么为什么我需要在这里使用X:X来集中我的组件?

1 个答案:

答案 0 :(得分:1)

这里重要的一点是首选尺寸。在您的另一个问题中,您说您使用以下代码来使其工作。

locationPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));

如果你写了

,这也会奏效
locationPanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]"));
//Obviously you could use any number not just 0

您可以使用的选项是(其中X&lt; = Y&lt; = Z)

X:Y:Z
X:Y:
:Y:Z
:Y:
:Y

为什么会这样做?
这样做是因为列以相同的速率增长,当您在所有三个面板中设置第一列的首选大小时,您将初始大小设置为MigLayout,将初始列大小设置为首选大小。

在本质上,您将列设置为相同的大小,并且它们以相同的速率增长,因此它们保持相同的大小。因此他们保持一致。

工作示例
使用:Y,其中YFont Metrics

确定
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

public class MigLay extends JFrame {

    private MigLay() {
        super("Button Layout");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new MigLayout("wrap", //Layout Constraints
        "grow, fill", //Column Constraints
        "grow, fill")); //Row Constraints
        createPanels();
        pack();
        setMinimumSize(getSize()); //Sets minimum size to the preferred size. Remove or change this line if you do not want that to happen
        setVisible(true);
    }

    private void createPanels() {

        JPanel locationPanel = new JPanel();
        JPanel usagePanel = new JPanel();
        JPanel structuralAspectsPanel = new JPanel();

        //JLabels for font metrics
        JLabel one = new JLabel("This is the first of all labels");
        JLabel two = new JLabel("Second Label");
        JLabel three = new JLabel("This Label is fairly large and long and pushes the text around");
        JLabel four = new JLabel("A label in the usage panel");
        JLabel five = new JLabel("And another one and another one and another one");

        //Font Metrics
        FontMetrics metrics = three.getFontMetrics(three.getFont()); //Take longest label manually or dynamically (You will have to add that code)
        int width = metrics.stringWidth(three.getText());

        locationPanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
        locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));

        usagePanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
        usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));

        locationPanel.add(one);
        locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JSeparator(), "growx, span");
        locationPanel.add(two);
        locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JSeparator(), "growx, span");
        locationPanel.add(three);
        locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

        usagePanel.add(four);
        usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JSeparator(), "growx, span");
        usagePanel.add(five);
        usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
        usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");

        getContentPane().add(locationPanel);
        getContentPane().add(usagePanel);
    }

    public static void main(String[] args) {
        new MigLay();
    }
}