2013-07-31 40 views
3

我是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"); 
    } 
} 
+0

不错 - 但我不能重现错误行为你描述(至少不是你的代码段) – kleopatra

+0

我加满示例及其运行的截图。我不认为这是一种不当行为 - 只是我没有正确使用它。 – zmb

+0

意味着错误 - 与您的期望不同:-)感谢您的完整示例,没有尝试过长标签 – kleopatra

回答

2

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

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