2016-02-11 69 views
2

我想了解MigLayout,因此我想創建一個不同面板內的表。如何在MigLayout中的垂直單元內堆疊組件?

目前,它看起來像這樣: no Flow in y direction

細胞2包含3個標籤,這應該疊加。因此,我試圖給電池2它自己的佈局center2.setLayout(new MigLayout("flowy"));,但隨後其他部件得到雜亂:

Flow y, but cluttered

那麼,有沒有一種方式,堆疊在小區2垂直的3個標籤?

[編輯]: center1和center2應該有相同的高度!

我的示例類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"); 
    } 
} 

回答

3

要回答你的問題保持 「flowy」 行,並把 「增長」 到

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

成爲

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

「增長」限制告訴組件增加重量比較到單元中的其他組件。如果它是單元格中唯一的組件,它將填充單元格(這正是我想要的)。要更好地理解它,請在MigLayout構造函數中使用「debug」。這會爲您提供圍繞單元格的邊框(紅色)和單元格中的組件(藍色)。

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