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

时间:2023-02-09 15:24:26

I'm trying to understand the MigLayout, therefore I want to create a table with different panels inside.

我正在尝试理解MigLayout,因此我想创建一个包含不同面板的表。

At the moment it's looking like this: 如何在MigLayout中的垂直单元格内堆叠组件?

目前它看起来像这样:

The cell 2 contains 3 labels, which should stack. Therefore I tried to give cell 2 it's own layout with center2.setLayout(new MigLayout("flowy"));, but then the other components get cluttered:

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

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

So is there a way, to stack the 3 labels in cell 2 vertically?

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

[EDIT]: center1 and center2 should have the same height!

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

My example class PageThree.java:

我的示例类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 个解决方案

#1


3  

To answer your question keep the "flowy" line and put "grow" into

要回答你的问题,请保持“飘逸”的界限并将“成长”放入

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

to become

成为

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

The "grow" constraint tells the component to grow in weight compared to other components in the cell. If it is the only component in the cell, it will fill the cell (which is what I think you want). To understand it better use "debug" in the MigLayout constructor. This gives you borders around the cells (red) and the components in the cells (blue).

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

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

#1


3  

To answer your question keep the "flowy" line and put "grow" into

要回答你的问题,请保持“飘逸”的界限并将“成长”放入

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

to become

成为

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

The "grow" constraint tells the component to grow in weight compared to other components in the cell. If it is the only component in the cell, it will fill the cell (which is what I think you want). To understand it better use "debug" in the MigLayout constructor. This gives you borders around the cells (red) and the components in the cells (blue).

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

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