swing类不是抽象的,也不会重写抽象方法java错误Tictoe game
对于一个项目,我必须做一个Tictatcoe游戏。然而,我在使用ActionListener时遇到了问题。它带来的错误是类不是抽象的,并且不重写抽象方法
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GameGUI extends JFrame implements ActionListener{
private JFrame gFrame = new JFrame("TicTacToe");
private JButton [][] buttons = new JButton[3][3];
private JButton reset = new JButton("New Game");
public GameGUI() {
super("TicTacToe");
gFrame.setSize(500,380);
gFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
gFrame.setVisible(true);
gFrame.setResizable(false);
gameBoard();
}
// Set everything into the actual game board
private void gameBoard() {
JPanel mPanel = new JPanel(new BorderLayout());
JPanel options = new JPanel(new BorderLayout());
JPanel game = new JPanel(new GridLayout(3,3));
gFrame.add(mPanel);
mPanel.setPreferredSize(new Dimension(325,425));
options.setPreferredSize(new Dimension(300,50));
game.setPreferredSize(new Dimension(300,300));
mPanel.add(options, BorderLayout.NORTH);
mPanel.add(game, BorderLayout.CENTER);
options.add(reset, BorderLayout.NORTH);
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
buttons[i][j] = new JButton();
buttons[i][j].setText("");
buttons[i][j].setVisible(true);
buttons[i][j].addActionListener(this);
game.add(buttons[i][j]);
}
}
我不知道如何着手解决这个问题,因此非常感谢您的帮助,我对Java相当陌生,如果代码不太好,请原谅
# 1 楼答案
在这一行代码中
您的意思是
GameGui
对象应该在单击其中一个按钮时响应。但您还没有指定它将如何响应。换句话说,您需要编写一个方法,其中包含单击按钮时要运行的代码该方法的第一行应该是
因为这就是方法在^{} 接口中声明的方式。当您在类的声明中编写} 接口中列出的所有方法的实现。幸运的是,这样的方法只有一种,但您确实需要实现它
implements ActionListener
时,您承诺提供^{