I’ve tried a couple of ways to do this…Basically I’m trying to create a tic tac toe board for an assignment and maybe I’m missing something obvious, but I get a “not a statement” error when I try to create the buttons. Here’s the code I’ve got:
int rows = 3;
int cols = 3;
JPanel ticTacToeBoard = new JPanel();
ticTacToeBoard.setLayout(new GridLayout(3, 3));
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
JButton gameButton[i] = new JButton[];
ticTacToeBoard.add(gameButton[i]);
}
}
Thanks…
You need to declare your array somewhere:
Then in your loop:
For example:
You can also have a look at the Java tutorial on arrays.
Note: Is there a reason why you don’t use a
Listinstead of an array? If would make your life easier.