2013-07-31 41 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