【问题标题】:Is it possible to "reset" a MigLayout是否可以“重置” MigLayout
【发布时间】:2013-08-01 09:50:01
【问题描述】:

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

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

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

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

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

有什么方法可以告诉 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");
    }
}

【问题讨论】:

  • 很好,您找到了解决方案 - 但我无法重现您描述的错误行为(至少不是您的 sn-p)
  • 我添加了一个完整的示例和它运行的屏幕截图。我不认为这是一种不当行为——只是我没有正确使用它。
  • 意思是错误-​​与您的期望不同 :-) 感谢您提供完整示例,没有尝试过长标签

标签: java swing layout user-interface miglayout


【解决方案1】:

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

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

结果如下:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多