2013-07-18 52 views
2

我希望通過組件來劃分可用的總空間,具體取決於它們是縮小還是增長。這應該發生動態的意義;我沒有關於添加到屏幕上的組件數量的任何信息。組件應根據它們的屬性,保持自己的身高或生長:Swing + MigLayout,其他組件的動態增長和收縮

+++++++++++++++++++++++++++++++++ 
| +-----------------------+ | 
| |  don't grow  | | 
| +-----------------------+ | 
|        | 
| +-----------------------+ | 
| |      | | 
| |  growy   | | 
| |      | | 
| +-----------------------+ | 
|        | 
| +-----------------------+ | 
| |  don't grow  | | 
| +-----------------------+ | 
|        | 
| +-----------------------+ | 
| |  don't grow  | | 
| +-----------------------+ | 
|        | 
| +-----------------------+ | 
| |      | | 
| |  growy   | | 
| |      | | 
| +-----------------------+ | 
|        | 
| +-----------------------+ | 
| |  don't grow  | | 
| +-----------------------+ | 
+++++++++++++++++++++++++++++++++ 

我能夠創建以下結果:

Almost there.. but not quite

提供的是以下SSCCE

public class SimpleMain { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("MigLayout"); 

     JPanel panel = new JPanel(new MigLayout("debug, flowy", "grow", "grow")); 

     // 1st set of textfields 
     panel.add(new JTextField(), "shrink, growx, top"); 
     panel.add(new JTextField(), "grow, top"); 
     panel.add(new JTextField(), "shrink, growx, top"); 

     // 2nd set of textfields 
     panel.add(new JTextField(), "shrink, growx, top"); 
     panel.add(new JTextField(), "grow, top"); 
     panel.add(new JTextField(), "shrink, growx, top"); 

     frame.getContentPane().add(panel); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

} 

第二個和第五個文本字段按預期增長。其他人依照預期保持其規模,儘管他們的容器不「收縮」,並且正在處理與其他組件分開的空間。第二和第五個文本字段應該佔據所有「額外」空間。

+0

+1好問題 – mKorbel

回答

1

經過一番測試,我可能找到了適合我的問題中提到的要求的解決方案。

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

private static void createAndShowGUI() { 
    JFrame frame = new JFrame("MigLayout"); 

    JPanel panel = new JPanel(new MigLayout(new LC().debug(1000).flowY().fillX())); 

    // 1rd set of components 
    panel.add(new JLabel("Titel"), new CC().grow()); 
    panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space 

    // 2st set of components 
    panel.add(new JLabel("Titel"), new CC().grow()); 
    panel.add(new JTextField(), new CC().grow()); 
    panel.add(new JLabel("Description"), new CC().grow()); 

    // 3nd set of components 
    panel.add(new JLabel("progress"), new CC().grow()); 
    panel.add(new JProgressBar(), new CC().grow()); 
    panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space 


    frame.getContentPane().add(panel); 

    frame.pack(); 
    frame.setVisible(true); 
} 

結果:

Result

正如你可能會注意到,在屏幕上2個組件全部採取額外的空間,而其他組件都留給它們的默認大小。