2011-11-08 43 views
1

我有一個主框架和3個面板。我想將這3個面板添加到主框架中。但是,其中只有2個被添加。第三個不是。我無法弄清楚爲什麼。有人可以幫忙嗎?面板不添加到主框架

 setLayout(new GridBagLayout()); 
     GridBagConstraints gbc=new GridBagConstraints(); 
     gbc.gridwidth=GridBagConstraints.REMAINDER; 
     gbc.gridheight=GridBagConstraints.RELATIVE; 
     gbc.anchor=GridBagConstraints.NORTHWEST; 
     gbc.fill=GridBagConstraints.BOTH; 
     gbc.weightx=gbc.weighty=1;  
     add(topPanel1, gbc); 
     add(bottomPanel1, gbc); 
     gbc.gridheight=GridBagConstraints.REMAINDER; 
     add(buttonsPanel, gbc); 

上面的代碼是一個框架的構造函數。

回答

1

應設置gridxgridy值,根據您所需的佈局。 例如如果您想佈局組件垂直做這樣的事情:

gbc.gridx=0; 
gbc.gridy=0; 
add(topPanel1, gbc); 
gbc.gridy++; 
add(bottomPanel1, gbc); 
gbc.gridy++; 
add(buttonsPanel, gbc); 

如果不設置gridx/y值的行爲是不確定的(有時它可能工作)。

1

您必須刪除gbc.gridheight=GridBagConstraints.RELATIVE;聲明,如果你想一個一個後加三個小組。