Java swing で Tic Tac Toe プログラムを作成しようとしていて、フレームを作成しました。JButton 配列のボタンで int 配列をアクティブにするにはどうすればよいですか? Tic Tac Toe グリッドのスポットの値を int 配列に保持したいので、ボタンが押されると、int 配列の対応するスポットが 0 または 1 になり、ボタンのテキストが次のように変わります。 Xまたは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);
}
}