2011-04-09 93 views
0

我正在用som按鈕製作面板。 this is what it looks like now.gridbadlayout垂直間隙

但我希望按鈕彼此接觸,換句話說,我希望垂直差距消失。但按鈕不應調整大小。我該怎麼去做這件事?

我的代碼:

this.setLayout(new GridBagLayout()); 
    GridBagConstraints constraints = new GridBagConstraints(); 

    constraints.fill = GridBagConstraints.HORIZONTAL; 
    constraints.gridwidth = 2; 
    constraints.weightx = 1D; 
    constraints.weighty = 1D; 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
    int row = 0; 
    for (MainAction actie : mainActions) { 
     constraints.gridy = row; 
     row++; 
     SelectedActionButton but = new SelectedActionButton(selectedActionModel, actie); 
     this.add(but, constraints); 

    } 
    constraints.gridwidth = 1; 
    constraints.weightx = 0.5D; 
    row *=2; 
    for (AbstractAction actie : objectActions) { 
     constraints.gridy = row/2; 
     constraints.gridx = row%2; 
     row++; 
     SelectedObjectButton but = new SelectedObjectButton(selectedObjectModel, actie); 
     but.setMargin(new Insets(0, 0, 0, 0)); 
     this.add(but, constraints); 
    } 

在此先感謝

回答

1

更改以下行

constraints.weighty = 1D; 

constraints.weighty = 0D; 

將解決您的問題。

更新:調整所有按鈕北您可以在最後一行添加一個附加組件吃的所有可用空間:

constraints.weighty = 1D; 
constraints.gridy = row; 
this.add(Box.createGlue(), constraints); 
+0

哦yups,這有點兒似乎工作:D只有一個問題,現在,如何我可以確保按鈕是北?因爲他們現在被集中 – Drareeg 2011-04-09 08:49:08

+1

@ user545236對於可能的北方解決方案,請參閱我的編輯。 – Howard 2011-04-09 08:56:10