2013-06-28 49 views
0

我很難讓GridLayout正常工作。我用GridLayout(3,3,10,10),但結果只顯示2列而不是3GridLayout(3,3,10,10)只顯示2列而不是3

這裏是我的代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Bunch extends JFrame { 
    JButton m=new JButton("M"); 
    JButton c=new JButton("C"); 
    JButton g=new JButton("G"); 
    JButton j=new JButton("J"); 
    JButton k=new JButton("K"); 
    JButton h=new JButton("H"); 

    public Bunch() { 
     super("Bunch"); 
     setSize(260,260); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel pane=new JPanel(); 
     GridLayout family=new GridLayout(3,3,10,10; 
     pane.setLayout(family); 
     pane.add(m); 
     pane.add(c); 
     pane.add(g); 
     pane.add(j); 
     pane.add(k); 
     pane.add(h); 
     add(pane); 
     setVisible(true); 
    } 

    public static void main(String[] arguments) { 
     Bunch frame=new Bunch(); 
    } 
} 
+0

您是否錯過了以下小括號:GridLayout family = new GridLayout(3,3,10,10; – cruxi

回答

3

這是正常的,你加6級的部件,但對於6個組成部分,你只需要2列。您需要爲3列添加3個組件或將GridLayout更改爲:GridLayout(2,3,10,10)

+0

)要說這個。+1給你,先生! – Tdorno

+0

哦,我明白了!非常感謝! – user2533006

+0

請[接受](http://meta.stackexchange.com/a/65088/155831)答案,如果它有助於解決問題。 –