2016-04-10 57 views
-3
JButton btn = new JButton(); 
    JButton[][] boutons = {{btn},{btn}}; 

    public Fenetre() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new GridLayout(9, 9)); 

     setContentPane(contentPane); 
     for (int ligne=1;ligne<=9;ligne++) { 
      for(int colone=1;colone<=9;colone++) { 
       //btn = new JButton(); 
       contentPane.add(boutons[ligne][colone]); 
       btn.setName(String.valueOf(ligne) + "" + String.valueOf(colone)); 
       System.out.println(String.valueOf(ligne) + "" + String.valueOf(colone)); 
       btn.addActionListener(this); 
      } 
     } 

我的錯誤來自哪裏?我不覺得我的異常的原因,請大家幫我Java異常ArrayIndexOutOfBounds

java.lang.ArrayIndexOutOfBoundsException:1

+0

我不是一個Java程序員,但猜想,有效9元素的數組索引是從0到8 –

+0

請給我校正線 –

+0

'對的代碼(INT LIGNE = 0; LIGNE <= 8; LIGNE ++){ 對(INT加冒號= 0;加冒號<= 8;加冒號++){' –

回答

0

您索引boutons出界。

boutons是大小2×1的二維陣列,從而只有有效的指標是:

boutons[0][0]boutons[1][0]

和你迭代[1..9] [1..9]爲boutons[ligne][colone]

0

你這樣做:

for (int ligne=1;ligne<=9;ligne++) { 

在一個不包含太多元素的數組..

JButton[][] boutons = {{btn},{btn}}; 

你需要循環在爲根據終扣陣列的lenght ..

JButton[][] boutons = {{btn},{btn}}; 
相關問題