我想用java swing製作一個Tic Tac Toe程序,並且我製作了框架。我怎麼能讓JButton數組中的按鈕激活int數組?我想要int數組來保存井字格網格中的點的值,所以當按下按鈕時,int數組中相應的點將是0或1,並且按鈕的文本將變爲一個X或O.JButton數組到int數組
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);
}
}