2013-12-11 19 views
0

問題用下面的代碼是,它是在半分裂MenuPane而不是所期望的10%至90%的JPanel GridBagLayout的未對準的垂直對象

TitlePaneControlPane只是JPanels。

public class MenuPane extends JPanel { 

public MenuPane(int width, int height){ 

    setSize(width, height); 

    setLayout(new GridBagLayout()); 

    GridBagConstraints c = new GridBagConstraints(); 

    c.gridx = 0; 
    c.gridy = 0; 

    c.weightx = 0.0d; 
    c.weighty = 0.1d; 

    add(new TitlePane(this.getWidth(), (int) (this.getHeight()*c.weighty)), c); 


    c.gridx = 0; 
    c.gridy = 1; 

    c.weightx = 0.0d; 
    c.weighty = 0.9d; 

    add(new ControlPane(this.getWidth(), (int) (this.getHeight()*c.weighty)), c); 

} 

} 

回答

2
add(new TitlePane(this.getWidth(), (int) (this.getHeight()*c.weighty)), c); 

真的不知道該代碼做什麼,但直到組件顯示在一個可見的GUI組件沒有大小。所以,你的getWidth()/身高()方法將返回0

如果你要顯示的組件在90/10的比例,則初始首選的大小也必須是在90/10的比例。重量因素僅適用於組件大小調整的情況。

我不知道你的ControlPane的代碼是什麼樣子,所以我不能提出一個具體的變化作出。

你可以嘗試使用它爲這個特別設計的Relative Layout。您只需在將組件添加到容器時指定配給量,並從那裏管理大小。

+0

將首選大小設置工作!謝謝! – MichaelMitchell