2

I am having a problem with a GUI constructor I am working on. It is supposed to be the GUI to a tic tac toe game, but none of my buttons are being created, and my GUI window is blank. I am really confused. I create an instance of the TicTacToePanel and add it to the main JFrame.

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
        }
    }
}
}
4
  • 1
    Can't find any TicTacToePanel constructor which calls the button creation routine !! Commented Feb 20, 2012 at 4:50
  • Where is TicTacToeCell defined and what does it do? Some more code would help. Commented Feb 20, 2012 at 4:51
  • 1
    Just because you name something "ButtonConstructor" doesn't make it a constructor. So don't call it a constructor (that will just confuse things); it is just any old method that you would have to call explicitly. You haven't included enough relevant information or code nor described the nature of your problem enough to expect much help. Commented Feb 20, 2012 at 4:52
  • Where is the constructor..? Commented Feb 20, 2012 at 4:54

1 Answer 1

6

The function you have, despite being called ButtonConstructor, is not a constructor.

In Java, a constructor must share the name of its parent class (and have no return type). The correct signature would be public TicTacToePanel().

I cannot say for sure without seeing a more complete view of your code (you have surely omitted most of it), but it is likely that you are not calling the function with which you provided us at all, but rather using the implied no-argument constructor. Try renaming the function to the signature I gave above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.