I am trying to make a Tic Tac Toe program with java swing, and I have the frame made. How can I go about making the buttons in the JButton array activate the int array? I want the int array to hold the values for the spots in the Tic Tac Toe grid, so when a button is pressed, the corresponding spot in the int array will be a 0 or a 1, and the text of the button will change to an X or an O.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacToeGui extends javax.swing.JFrame implements ActionListener
{
int[][] grid = new int[3][3];
public final static int r = 3;
public final static int c = 3;
public final static int X = 0;
public final static int O = 1;
TicTacToeGui()
{
this.setTitle("Tic Tac Toe");
JButton[][] button = new JButton[3][3];
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(r, c));
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
button[i][j] = new JButton("");
button[i][j].addActionListener(this);
panel.add(button[i][j]);
}
}
this.add(panel);
this.setSize(400, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof JButton){
}
}
public static void main(String [] args)
{
new TicTacToeGui().setVisible(true);
}
}