如何在MigLayout中的垂直单元格内堆叠组件?

时间:2016-02-11 10:45:19

标签: java swing layout-manager miglayout

我试图理解MigLayout,因此我想创建一个内部有不同面板的表。

目前它看起来像这样: no Flow in y direction

单元格2包含3个标签,应该堆叠。因此,我尝试使用center2.setLayout(new MigLayout("flowy"));为单元格2提供自己的布局,但随后其他组件变得混乱:

Flow y, but cluttered

那么有没有办法将3个标签垂直堆叠在单元格2中?

[编辑]: center1和center2应该有相同的高度!

我的示例类 PageThree.java:

public class PageThree extends JPanel{

    public PageThree() {
        setLayout(new MigLayout());
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new MigLayout("width 100%"));

        JPanel topHeading = new JPanel();
        JPanel westAreas = new JPanel();
        JPanel center1 = new JPanel();
        JPanel center2 = new JPanel();

        // If I give center2 its own layout with flowy, 
        // the 3 labels are stacked vertically, 
        // but the other components get cluttered

        // center2.setLayout(new MigLayout("flowy"));
        JPanel center3 = new JPanel();

        center1.add(new JLabel("center1"));

        center2.add(new JLabel("center21"));
        center2.add(new JLabel("center22"));
        center2.add(new JLabel("center23"));

        center3.add(new JLabel("center3"));
        topHeading.add(new JLabel("topHeading1"));
        topHeading.add(new JLabel("topHeading2"));
        topHeading.add(new JLabel("topHeading3"));
        westAreas.add(new JLabel("westAreas"));

        contentPanel.add(center1, "width 40%");
        contentPanel.add(center2, "width 60%, wrap");
        contentPanel.add(center3, "width 50%");
        contentPanel.add(topHeading, "width 100%, dock north, split 3");
        contentPanel.add(westAreas, "dock west");
    }
}

1 个答案:

答案 0 :(得分:3)

要回答您的问题,请保持"流畅"排队"成长"进入

contentPanel.add(center1, "width 40%");

成为

contentPanel.add(center1, "width 40%, grow");

"成长"约束告诉组件与单元中的其他组件相比增重。如果它是单元格中唯一的组件,它将填充单元格(这是我认为你想要的)。为了更好地理解它,使用" debug"在MigLayout构造函数中。这样可以为单元格(红色)和单元格中的组件(蓝色)提供边框。

contentPanel.setLayout(new MigLayout("width 100%, debug"));