java - 是否有可能 "reset"一个 MigLayout

标签 java swing layout user-interface miglayout

我是 MigLayout 的新手。我正在尝试创建与此类似的布局:

enter image description here

有一排等间距的按钮,后面是一行列表,该列表跨越所有列并增长以填充可用的垂直空间,最后一行包含更多控件。

我可以毫不费力地获得前两行。

但是,当我添加最后一行的内容时,MigLayout(理所当然地)尝试保留前两行的列。我留下了这样的东西:

enter image description here

最后一行的标签和微调器扩展了列的宽度,我在第一行留下了不均匀的间隙。

有什么方法可以告诉 MigLayout 我想忘记到目前为止建立的行/列并“重新开始”,或者这里的解决方案是创建嵌套布局吗?

这是一个完整的示例面板。

public class TestPanel extends JPanel {

    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    JButton button3 = new JButton("Button 3");
    JButton button4 = new JButton("Button 4");
    JButton button5 = new JButton("Button 5");

    JList list = new JList(new String[]{"some", "fake", "data"});

    JLabel label = new JLabel("this is my long label");
    JSpinner spinner = new JSpinner();
    JCheckBox checkbox = new JCheckBox("Check me");

    public TestPanel() {
        initComponents();
    }

    private void initComponents() {

        setLayout(new MigLayout());

        add(button1);
        add(button2);
        add(button3);
        add(button4);
        add(button5, "wrap");

        add(list, "span, growx, growy, wrap");

        // without these 3 lines, the row of buttons are equally spaced
        // adding the long label extends the width of the first column
        add(label);
        add(spinner);
        add(checkbox, "span, align right");
    }
}

最佳答案

我能够通过合并和拆分单元格来实现所需的布局。

setLayout(new MigLayout());
add(button1);
add(button2);
add(button3);
add(button4);
add(button5, "wrap");
add(list, "span, growx, growy, wrap");

// merge 4 cells then split the combined cell in half
// label goes in the first cell of the split
// spinner goes in the second cell of the split
add(label, "span 4, split 2);
add(spinner);
// check box goes in the 5th and final cell of the row (after the 4 merged cells)
add(checkBox, "align right");

结果如下:

enter image description here

关于java - 是否有可能 "reset"一个 MigLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17977685/

相关文章:

java - 如何检查两个 boolean 表达式是否相等

java - 无法重新添加相同的面板

android - match_parent 破坏完整布局

java - 序列号的 JPA 序列

java - 摆脱java中的最后几个错误

java - 禁用 DataGrid 的特定列

java - 如何在小屏幕布局上禁用横向

java - 使用 Java 布局管理器的目的是什么?

java - 使用 java 将新标签附加到现有 xml

java - 为什么我的 jframe 不出现?