我正在尝试构建一个扫雷类型的游戏,更具体地说,是曾经在 MSN 游戏中出现的两个玩家。我有一个 Tile 对象的多维数组。每个 Tile 都有一个状态(地雷、空白或相邻的地雷数量)。我有一个处理程序所有前端方面的 GUI 类。
每个 Tile 都扩展了 JButton 并实现了 MouseListener,但是当我单击一个按钮时,它不会触发相应按钮/tile 的 MouseClicked 方法。
代码如下:
public class Tile extends JButton implements MouseListener {
private int type;
public Tile(int type, int xCoord, int yCoord) {
this.type = type;
this.xCoord = xCoord;
this.yCoord = yCoord;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
}
}
和 GUI 类:
public class GUI extends JPanel {
JFrame frame = new JFrame("Mines");
private GameBoard board;
private int width, height;
public GUI(GameBoard board, int width, int height) {
this.board = board;
this.width = width;
this.height = height;
this.setLayout(new GridLayout(board.getBoard().length, board.getBoard()[0].length));
onCreate();
}
private void onCreate() {
for (int i = 0; i < board.getBoard().length; i++) {
for (int j = 0; j < board.getBoard()[i].length; j++) {
this.add(board.getBoard()[i][j]);
}
}
frame.add(this);
frame.setSize(width, height);
frame.setMinimumSize(this.minFrameSize);
frame.setPreferredSize(new Dimension(this.width, this.height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
GUI 类 JPanel 是否拦截阻止按钮接收单击事件的 MouseClick 事件?