2012-02-20 4 views
1

작업중인 GUI 생성자에 문제가 있습니다. 그것은 tic tac toe 게임에 대한 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

감사합니다. 문제를 해결 한 것으로 보입니다. –

관련 문제