我想制作TIC TAC TOE游戏,但有问题。我制作了一个2D数组,但我不知道如何寻址它们才能使ActionListener
工作。
这是我的代码:
public class GUI {
public static JButton[][] buttonsall = new JButton[3][3];
public static JFrame frame = new JFrame("TIC TAC TOE");
public static void draw() {
// frame
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setBackground(Color.white);
frame.setBounds(500, 500, 600,600);
// actionListener
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
//dont know further here
}
}
};
//buttons buttonsall[x][y]
for (int y = 0; y<3;y++) {
for (int x = 0; x<3;x++) {
buttonsall[x][y] = new JButton();
buttonsall[x][y].setVisible(true);
buttonsall[x][y].setSize(80, 80);
buttonsall[x][y].addActionListener(listener);
System.out.println(buttonsall[x][y] +" "+x +" "+y);
frame.add(buttonsall[x][y]);
buttonsall[x][y].setBackground(Color.white);
}
}
frame.setLayout(new GridLayout(3,3));
}
您可以在创建每个JButton时为每个JButton设置setName。我将更新您的代码,如下所示:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI {
public static JButton[][] buttonsall = new JButton[3][3];
public static JFrame frame = new JFrame("TIC TAC TOE");
public static void draw() {
// frame
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setBackground(Color.white);
frame.setBounds(500, 500, 600, 600);
// actionListener
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
//dont know further here
JButton b = (JButton)e.getSource();
System.out.println("Button is:" + b.getName());
}
}
};
//buttons buttonsall[x][y]
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
buttonsall[x][y] = new JButton();
buttonsall[x][y].setVisible(true);
buttonsall[x][y].setName("Button_" + x + "_" + y);
buttonsall[x][y].setSize(80, 80);
buttonsall[x][y].addActionListener(listener);
System.out.println(buttonsall[x][y] + " " + x + " " + y);
frame.add(buttonsall[x][y]);
buttonsall[x][y].setBackground(Color.white);
}
}
frame.setLayout(new GridLayout(3, 3));
}
public static void main(String[] args) {
GUI.draw();
}
}
您的问题与this question非常相似。
基本上,您想使用ActionEvent.getSource()
并将其转换为JButton
。然后,您可以对JButton
进行任何操作(更改背景颜色,更改文本等)。
编辑:我没有意识到您想要获得JButton的x
和y
。
public class TicTacToe {
public static TicTacToeButton[][] buttonsall = new TicTacToeButton[3][3];
public static JFrame frame = new JFrame("TIC TAC TOE");
public static void draw() {
// frame
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setBackground(Color.white);
frame.setBounds(500, 500, 600,600);
// actionListener
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof TicTacToeButton) {
TicTacToeButton btn = (TicTacToeButton) source;
int btnBoardX = btn.getBoardX();
int btnBoardY = btn.getBoardY();
// Go ahead and do what you like
}
}
};
//buttons buttonsall[x][y]
for (int y = 0; y<3;y++) {
for (int x = 0; x<3;x++) {
buttonsall[x][y] = new TicTacToeButton(x, y);
buttonsall[x][y].setVisible(true);
buttonsall[x][y].setSize(80, 80);
buttonsall[x][y].addActionListener(listener);
System.out.println(buttonsall[x][y] +" "+x +" "+y);
frame.add(buttonsall[x][y]);
buttonsall[x][y].setBackground(Color.white);
}
}
frame.setLayout(new GridLayout(3,3));
}
private static class TicTacToeButton extends JButton {
private int boardX;
private int boardY;
public TicTacToeButton(int boardX, int boardY) {
super();
this.boardX = boardX;
this.boardY = boardY;
}
public int getBoardX() {
return this.boardX;
}
public int getBoardY() {
return this.boardY;
}
}
}