如何使用MigLayout将内容集中在多个面板上?

时间:2016-06-07 09:53:42

标签: java centering miglayout

我有一个大面板,其中包含3个较小的面板(locationPanelusagePanelstructuralAspectsPanel)。

每个较小的面板上都有一些JLabelsJCheckBoxes。我继续将每个面板上的组件集中在一起,但是如何在所有3个面板上实现它们的中心? (请参阅中心的黑线)

我尝试在MigLayoutnew JLabel("Label here"), "cell 0 0")中使用单元格布局选项,但无法创建相同大小的动态间隙,因此所有组件都居中。使用{{1 }}(180,300,...)to" push"组件进入视觉中心似乎有效,但我想避免使用绝对定位/间隙,因为它们可能很容易破坏。

我附上了我的问题图片: How to center the components over several panels?

这是我的源代码:

gap 200

2 个答案:

答案 0 :(得分:1)

为了使面板列对齐,您需要将它们设置为相同的大小。在MigLayout中,您可以通过将第一列的首选尺寸增长率设置为与您相同的值来实现此目的所有面板。例如

panel1.new MigLayout("", //Unchanged Layout Constraints
        "[:500, grow, center][grow, left]", //Column Constraints
        ""); //Unchanged Row Constraints

panel2.new MigLayout("", //Unchanged Layout Constraints
        "[:500, grow, center][grow, left]", //Column Constraints
        ""); //Unchanged Row Constraints

在此示例中,panel1panel2的第一列的首选大小设置为500,增长设置为默认值,因此也是相同的。

这里可以看到所描述内容的工作示例

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();
    }
}

答案 1 :(得分:0)

我已经弄明白了,实际上很简单!

0:0, grow 60添加到第一列,将grow 40添加到第二列。

grow 60 / 40 - 部分表示左列获得可用宽度的60%,而另一列获得40%。

0:0 - 部分是关于设置最小/首选/最大宽度(尽管我必须承认我完全不理解它,解释欢迎!...我' ve opened a question就这个。)。

由于所有面板左右两侧的尺寸相同,因此它们居中于同一点。

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]"));