获取错误:无法实例化类型java.awt.event.ActionListener

获取错误:无法实例化类型java.awt.event.ActionListener,java,swing,awt,instance,actionlistener,Java,Swing,Awt,Instance,Actionlistener,所以我正在尝试做一个Tic-Tac-Toe游戏,我对JFrames、panel和几乎所有的GUI东西都是全新的。我希望这不是一个重复,因为我扫描了这个网站几个小时试图找到一个答案。这很好,因为我是新来的,可能有一个答案,但我不明白。无论如何,这个错误在标题中有说明,我的目标是找出如何检测单击了哪个按钮,然后使用if/else语句来控制接下来发生的事情,方法如下。我意识到一些导入的内容没有被使用,但我计划一旦我进入程序,它们可能会被使用。再一次,我对荡秋千和它周围的一切都是新手。我的大部分知识都是

所以我正在尝试做一个Tic-Tac-Toe游戏,我对JFrames、panel和几乎所有的GUI东西都是全新的。我希望这不是一个重复,因为我扫描了这个网站几个小时试图找到一个答案。这很好,因为我是新来的,可能有一个答案,但我不明白。无论如何,这个错误在标题中有说明,我的目标是找出如何检测单击了哪个按钮,然后使用if/else语句来控制接下来发生的事情,方法如下。我意识到一些导入的内容没有被使用,但我计划一旦我进入程序,它们可能会被使用。再一次,我对荡秋千和它周围的一切都是新手。我的大部分知识都是自学的,所以任何帮助都是非常感谢的

    import java.util.Scanner;
    import java.lang.Object;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Window;
    import java.awt.Frame;
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;


public class ticTacToe implements ActionListener //uses action listener because of the use of buttons, so it needs to know when the buttons are clicked
{
  public static JFrame menuFrame = new JFrame("Tic-Tac-Toe");
  public static JPanel board = new JPanel(), menu = new JPanel();
  public static JButton instruct = new JButton("Instructions"), pVP = new JButton("Player VS. Player"), pVC = new JButton("Player VS. Computer");

  public static void main (String[]args)
  {
    menu();
  }
  public static void menu ()//the main menu of the game
  {
    menu.setLayout(new FlowLayout());//arranges the layout of the buttons on the panel

    menu.add(instruct);//adds the instruction button

    menu.add(pVP);//adds the player vs player button
    menu.add(pVC);//adds the player vs computer button

    menuFrame.add(menu);//creates the panel
    menuFrame.setSize(450, 78);
    menuFrame.setLocationRelativeTo(null);//sets the location to the centre of the screen
    menuFrame.setVisible(true);//makes the menu visible
  }
   public void actionPerformed(ActionEvent actionEvent) {
instruct.addActionListener(new ActionListener());
          System.out.println(actionEvent.getActionCommand());
      }
}

这是一行
指令.addActionListener(newActionListener())

ActionListener
是一个必须在子类中实现的接口

为了使其有意义,最简单的修复方法是将该行更改为
instruction.addActionListener(this)
,并将其移动到构造函数中,因为您的类已经实现了
ActionListener
。如果使用此解决方案,游戏逻辑代码将被移动到
Menu()
类的
actionPerformed()
方法中。或者您可以创建一个新类来实现它,游戏逻辑将在其中:

public class TicTacToeListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // game logic here
    }
}

public class ticTacToe implements ActionListener {
    public void actionPerformed(ActionEvent actionEvent) {
        instruct.addActionListener(new TickTacToeListener);
        System.out.println(actionEvent.getActionCommand());
    }
}

那我在这里该怎么办?还有,有没有什么办法我不能用“this”,因为我从来没有用过,所以我真的不知道它是什么意思。。?或者你知道一种更简单的方法来写这个方法吗?我试图把它作为一种方法,但我愿意使用一个类。谢谢
public void actionPerformed(ActionEvent ActionEvent){instruction.addActionListener(new ActionListener());System.out.println(ActionEvent.getActionCommand());}
对不起,我修复了第一个解决方案<代码>动作监听器
s绑定到Swing GUI组件。
ActionListener
的每个实例都有自己的
actionPerformed()
方法,当某个操作触发该组件时会调用该方法。通过在
ticTacToe
类中实现
ActionListener
,您可以保证
actionPerformed
是类的一部分。当你说
addActionlistener(this)
时,你要求你正在添加的组件在
菜单的当前实例中调用
actionPerformed()
,那么我需要更改我的类的名称吗?或者我可以只更改
instruction.addActionListener(newActionListener())
指令.addActionListener(此)在方法中?为了让它运行,您必须将其更改为第二个,但这没有任何意义,而且您很可能不会得到您想要的结果。如果您了解什么是多态性和接口,它将更有意义。多态性:接口: