2012-02-20 16 views
1

私が取り組んでいるGUIコンストラクタに問題があります。チックタックトーゲームのGUIになっていますが、私のボタンは作成されておらず、GUIウィンドウも空白です。私は本当に混乱しています。私はTicTacToePanelのインスタンスを作成し、それをメインのJFrameに追加します。Java:コンストラクタが動作しません

class TicTacToePanel extends JPanel implements ActionListener { 

public void actionPerformed(ActionEvent e) { 
} 
//Creates the button array using the TicTacToeCell constructor 
private TicTacToeCell[] buttons = new TicTacToeCell[9]; 
//(6) this constructor sets a 3 by 3 GridLayout manager in the panel 
///then creates the 9 buttons in the buttons arrray and adds them to the panel 

//As each button is created 
///The constructer is passed a row and column position 
///The button is placed in both the buttons array and in the panels GridLayout 
///THen an actionListener this for the button 
public void ButtonConstructor() { 
    //creates the layout to pass to the panel 
    GridLayout mainLayout = new GridLayout(3, 3); 
    //Sets a 3 by 3 GridLayout manager in the panel 
    this.setLayout(mainLayout); 
    int q = 1; //provides a counter when creating the buttons 
    for (int row = 0; row < 3; row++) //adds to the current row 
    { 
     for (int col = 0; col < 3; col++) //navigates to the correct columns 
     { 
      System.out.println("Button " + q + " created"); 
      buttons[q] = new TicTacToeCell(row, col); 
      mainLayout.addLayoutComponent("Button " + q, this); 
      this.add(buttons[q]); //adds the buttons to the ticTacToePanel 
      buttons[q].addActionListener(this); //this sets the panel's action listener to the button 
      q++; //increments the counter 
     } 
    } 
} 
} 
+1

ボタン作成ルーチンを呼び出す 'TicTacToePanel'コンストラクタが見つかりません!! – Favonius

+0

「TicTacToeCell」はどこに定義されていますか?いくつかのコードが役に立ちます。 – casablanca

+1

「ButtonConstructor」という名前を付けても、コンストラクタにはなりません。だから、コンストラクタと呼んではいけません。明示的に呼び出さなければならない古いメソッドです。関連する情報やコードを十分に記載しておらず、多くの助けが必要なほど問題の性質を説明していません。 –

答えて

5

あなたが持っている機能は、ButtonConstructorと呼ばれていますが、コンストラクタではありません。

Javaでは、コンストラクタは親クラスの名前を共有する必要があります(戻り値の型はありません)。正しい署名はpublic TicTacToePanel()です。

コードの完全なビューを見ることなく(確かにそのほとんどを省略しています)、確かに言うことはできませんが、あなたが私たちに提供した関数を呼び出すのではなく、暗黙の引数なしコンストラクタ。上記の署名に関数の名前を変更してみてください。

+0

ありがとう、その問題を修正するようだ! –

関連する問題