2014-09-28 74 views
8

我試圖使用GridBagLayout,但我沒有得到我的期望,並在此代碼,我找不到錯誤:Java的GridBagLayout中不工作

public class GridBagEx1 extends JPanel { 

    private static final long serialVersionUID = 1L; 

    protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { 
     JButton button = new JButton(name); 
     gridbag.setConstraints(button, c); 
     add(button); 
    } 

    public void init() { 
     GridBagLayout gridbag = new GridBagLayout(); 
     GridBagConstraints c = new GridBagConstraints(); 
     setLayout(gridbag); 

     c.fill = BOTH; 

     c.weightx = 1.0; 
     c.weighty = 1.0; 

     c.anchor = CENTER; 

     c.insets.top = 5; 
     c.insets.bottom = 5; 
     c.insets.left = 5; 
     c.insets.right = 5; 

     c.gridx = 0; 
     c.gridy = 0; 
     c.gridheight = 1; 
     c.gridwidth = 2; 
     makebutton("Button1", gridbag, c);  

     c.gridx = 2; 
     c.gridy = 0; 
     c.gridheight = 1; 
     c.gridwidth = 1; 
     makebutton("Button2", gridbag, c); 

     c.gridx = 3; 
     c.gridy = 0; 
     c.gridheight = 2; 
     c.gridwidth = 2; 
     makebutton("Button3", gridbag, c); 

     c.gridx = 0; 
     c.gridy = 1; 
     c.gridheight = 1; 
     c.gridwidth = 1; 
     makebutton("Button4", gridbag, c); 

     c.gridx = 1; 
     c.gridy = 1; 
     c.gridheight = 1; 
     c.gridwidth = 2; 
     makebutton("Button5", gridbag, c); 

     c.gridx = 0; 
     c.gridy = 2; 
     c.gridheight = 1; 
     c.gridwidth = 1; 
     makebutton("Button6", gridbag, c); 

     c.gridx = 1; 
     c.gridy = 2; 
     c.gridheight = 1; 
     c.gridwidth = 2; 
     makebutton("Button7", gridbag, c); 

     c.gridx = 3; 
     c.gridy = 2; 
     c.gridheight = 1; 
     c.gridwidth = 1; 
     makebutton("Button8", gridbag, c); 

     c.gridx = 4; 
     c.gridy = 2; 
     c.gridheight = 1; 
     c.gridwidth = 1; 
     makebutton("Button9", gridbag, c); 
    } 

    public static void main(String args[]) { 
     JFrame frame = new JFrame(); 
     GridBagEx1 ex1 = new GridBagEx1(); 

     ex1.init(); 

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

這張照片說明我需要什麼:

Programmers love paint

黃色的按鈕名稱,紅色的行和列。

這到底發生了什麼: Whats wrong here?

誰能解釋一下什麼是錯在我的代碼?

+4

我無法解釋這個問題呢,但必須指出你的問題是很好的呈現,謝謝你和1+。 – 2014-09-28 19:32:56

+0

@HovercraftFullOfEels謝謝 – Emax 2014-09-28 19:35:23

+0

'c.fill = BOTH;'應該是'c.fill = GridBagConstraints.BOTH;'和'c.anchor = CENTER;'是相同的代碼才能編譯。 – user1803551 2014-09-28 20:18:26

回答

6

問題是沒有什麼能說服第二個網格列(gridx = 1)具有任何寬度,因爲沒有組件需要在第二列中適合只有。因此,第二列的寬度爲0,因此雖然Button1跨越前兩列,但它看起來並不那樣,因爲它的所有寬度需求都由第一列滿足;儘管Button5和Button7跨越第二和第三列,但它們的所有寬度需求都由第三列滿足。

要修復它,你必須說服按鈕應該是更寬(1,5,7)佔用更多的空間。在這裏,我通過設置c.ipadx = 35;向這些按鈕添加了填充。 (我也刪除了weightx = 1.0約束的原因,我不太瞭解,也沒有那個留在工作的時候。):

screenshot

來源:

public void init() { 
    GridBagLayout gridbag = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints(); 
    setLayout(gridbag); 

    c.fill = c.BOTH; 

    //c.weightx = 1.0; 
    //c.weighty = 1.0; 

    c.anchor = c.CENTER; 

    c.insets.top = 5; 
    c.insets.bottom = 5; 
    c.insets.left = 5; 
    c.insets.right = 5; 

    c.gridx = 0; 
    c.gridy = 0; 
    c.gridheight = 1; 
    c.gridwidth = 2; 
    c.ipadx = 35; 
    makebutton("Button1", gridbag, c); 

    c.gridx = 2; 
    c.gridy = 0; 
    c.gridheight = 1; 
    c.gridwidth = 1; 
    c.ipadx = 0; 
    makebutton("Button2", gridbag, c); 

    c.gridx = 3; 
    c.gridy = 0; 
    c.gridheight = 2; 
    c.gridwidth = 2; 
    c.ipadx = 0; 
    makebutton("Button3", gridbag, c); 

    c.gridx = 0; 
    c.gridy = 1; 
    c.gridheight = 1; 
    c.gridwidth = 1; 
    c.ipadx = 0; 
    makebutton("Button4", gridbag, c); 

    c.gridx = 1; 
    c.gridy = 1; 
    c.gridheight = 1; 
    c.gridwidth = 2; 
    c.ipadx = 35; 
    makebutton("Button5", gridbag, c); 

    c.gridx = 0; 
    c.gridy = 2; 
    c.gridheight = 1; 
    c.gridwidth = 1; 
    c.ipadx = 0; 
    makebutton("Button6", gridbag, c); 

    c.gridx = 1; 
    c.gridy = 2; 
    c.gridheight = 1; 
    c.gridwidth = 2; 
    c.ipadx = 35; 
    makebutton("Button7", gridbag, c); 

    c.gridx = 3; 
    c.gridy = 2; 
    c.gridheight = 1; 
    c.gridwidth = 1; 
    c.ipadx = 0; 
    makebutton("Button8", gridbag, c); 

    c.gridx = 4; 
    c.gridy = 2; 
    c.gridheight = 1; 
    c.gridwidth = 1; 
    c.ipadx = 0; 
    makebutton("Button9", gridbag, c); 
} 

編輯:正如在評論中指出的那樣,上述方法並不合適,因爲它阻止了動態調整佈局。爲了使佈局擴大以填充其容器的大小,需要weightxweighty約束,但第二列沒有得到任何寬度。

這是一個替代解決方案的嘗試。它使得在第二塔的底部插入的不可見部件的骯髒的黑客以迫使柱具有寬度:

c.gridx = 1; 
    c.gridy = 3; 
    c.gridheight = 1; 
    c.gridwidth = 1; 
    c.insets.set(0, 0, 0, 0); 
    c.weighty = 0; 
    add(Box.createRigidArea(new Dimension(50, 0)), c); 

這科佩斯相當好時雖然部件被賦予一個固定的初始大小的窗口,因爲調整大小,GridBagLayout按比例與其他組件按比例放大。儘管如此,它仍然不完美。也許有更好的解決方案,但我找不到它。

+0

我可以看到源代碼嗎? – Emax 2014-09-28 21:05:37

+0

@FilipB.Vondrášek我不認爲這是一個特定的(相當複雜的)佈局管理員的特例,並且認爲Swing是一種可憎的東西。當然,Swing有它的缺點和優點,但它不像是一個祕密,每個人都知道。 – icza 2014-09-28 21:08:08

+0

@Emax新增 – Boann 2014-09-28 21:08:35

2

我管理,沒有任何黑客,支持動態調整大小來設計需要佈局使用JGoodies FormLayout

import java.awt.Component; 

import javax.swing.JButton; 
import javax.swing.JPanel; 

import com.jgoodies.forms.factories.FormFactory; 
import com.jgoodies.forms.layout.ColumnSpec; 
import com.jgoodies.forms.layout.FormLayout; 
import com.jgoodies.forms.layout.RowSpec; 

public class FormLayoutPanel extends JPanel 
    { 
    public FormLayoutPanel() 
     { 
     setAlignmentY(Component.BOTTOM_ALIGNMENT); 
     setAlignmentX(Component.RIGHT_ALIGNMENT); 
     setLayout(new FormLayout(new ColumnSpec[] { 
      ColumnSpec.decode("41px:grow"), 
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 
      ColumnSpec.decode("25px:grow"), 
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 
      ColumnSpec.decode("41px:grow"), 
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 
      ColumnSpec.decode("41px:grow"), 
      FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 
      ColumnSpec.decode("41px:grow"), }, 
      new RowSpec[] { 
       RowSpec.decode("25px:grow"), 
       FormFactory.LINE_GAP_ROWSPEC, 
       RowSpec.decode("25px:grow"), 
       FormFactory.LINE_GAP_ROWSPEC, 
       RowSpec.decode("25px:grow"), })); 

     JButton button1 = new JButton("1"); 
     add(button1, "1, 1, 3, 1, fill, fill"); 
     JButton button2 = new JButton("2"); 
     add(button2, "5, 1, fill, fill"); 
     JButton button3 = new JButton("3"); 
     add(button3, "7, 1, 3, 3, fill, fill"); 
     JButton button4 = new JButton("4"); 
     add(button4, "1, 3, fill, fill"); 
     JButton button5 = new JButton("5"); 
     add(button5, "3, 3, 3, 1, fill, fill"); 
     JButton button6 = new JButton("6"); 
     add(button6, "1, 5, fill, fill"); 
     JButton button7 = new JButton("7"); 
     add(button7, "3, 5, 3, 1, fill, fill"); 
     JButton button8 = new JButton("8"); 
     add(button8, "7, 5, fill, fill"); 
     JButton button9 = new JButton("9"); 
     add(button9, "9, 5, fill, fill"); 
     } 
    } 
+0

我會試試這個解決方案,謝謝你的幫助! – Emax 2014-10-09 21:55:28