[英]class is not abstract and does not override abstract method java error TIcTacToe game
For a project I have to make a tictactoe game. 对于一个项目,我必须做一个tictactoe游戏。 However I'm having a problem with using ActionListener. 但是我在使用ActionListener时遇到问题。 It brings up the error that class is not abstract and does not override abstract method. 它带来了类不是抽象并且没有覆盖抽象方法的错误。
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]);
}
}
I'm not sure how to go about and fix this so any help would be greatly appreciated, I'm fairly new to Java so please excuse if the code is not great. 我不确定该如何解决此问题,因此不胜感激,对于Java来说我还是一个新手,所以如果代码不是很好,请原谅。
In this line of code, 在这行代码中,
buttons[i][j].addActionListener(this);
you're saying that a GameGui
object should respond when one of the buttons is clicked. 您说的是当单击其中一个按钮时, GameGui
对象应该响应。 But you haven't specified how it will respond. 但是您尚未指定它将如何响应。 In other words, you need to write a method that contains the code that you want to run when a button is clicked. 换句话说,您需要编写一个方法,其中包含要在单击按钮时运行的代码。
The first line of that method should be 该方法的第一行应为
public void actionPerformed(ActionEvent e) {
because that's the way the method has been declared in the ActionListener
interface. 因为这是在ActionListener
接口中声明该方法的方式。 When you wrote implements ActionListener
in the declaration of your class, you promised to provide implementations of all the methods listed in the ActionListener
interface. 当您在类的声明中编写implements ActionListener
时,承诺将提供ActionListener
接口中列出的所有方法的实现。 Fortunately, there's only one such method, but you do need to implement it. 幸运的是,只有一种这样的方法,但是您确实需要实现它。
When you extend an abstract class or implement an interface, you need to complete the concrete class definition by providing definition to all those inherited abstract functions. 扩展抽象类或实现接口时,需要通过为所有那些继承的抽象函数提供定义来完成具体的类定义。
In this case, you are missing definition to the following method which is declared in ActionListener interface 在这种情况下,您缺少在ActionListener接口中声明的以下方法的定义
public void actionPerformed(ActionEvent e)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.