2016-12-14 98 views
0

我正在開發java井字棋遊戲。首先,我創建了一個按鈕類,然後我嘗試存儲該類的實例數組。一切工作正常,直到我將這些對象添加到框架。這裏是我的代碼:將對象存儲在數組中並顯示它們(Java)

package tictactoe; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
public class TicTacToe extends JFrame 
{ 

    TicTacToe() 
    { 
     this.setLayout(null); 
     this.setResizable(true); 
     this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); 
     this.setSize(500,500);  
     this.setBackground(Color.blue); 


     PlayingButton[] b = new PlayingButton [9]; 
     for (int i = 0 ; i < 9 ; i++) 
     { 
      b[i] = new PlayingButton(); 
     } 
     b[0].setBounds(0,0,50,50); 
     b[1].setBounds(50,0,50,50); 
     b[2].setBounds(100,0,50,50); 
     this.add(b[0]); 
     this.setVisible(true); 
    } 


    public static void main(String[] args) 
    { 
     TicTacToe board = new TicTacToe(); 

    } 

} 

,是造成我的問題行:

this.add(b[0]); 
+3

然後,會發生什麼?你期望代碼做什麼,它做什麼呢? –

+1

什麼是PlayingButton?發生了什麼?是否有堆棧跟蹤? [我怎麼問一個好問題?](http://stackoverflow.com/help/how-to-ask) – flakes

+1

PlayingButton是我創建它的另一個類,用於從中獲取對象。 –

回答

3

PlayingButton類應該擴展JComponent的或它的子類。

+0

它的工作原理,謝謝! –

相關問題