【问题标题】:Getting the Error: Cannot instantiate the type java.awt.event.ActionListener获取错误:无法实例化类型 java.awt.event.ActionListener
【发布时间】:2015-02-28 05:49:56
【问题描述】:

所以我正在尝试制作一款井字游戏,而且我对 JFrame、面板以及几乎所有的 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());
      }
}

【问题讨论】:

    标签: java swing awt instance actionlistener


    【解决方案1】:

    电话是instruct.addActionListener(new ActionListener());

    ActionListener 是您必须在子类中实现的接口。

    为了使这有意义,最简单的解决方法是将该行更改为instruct.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());
        }
    }
    

    【讨论】:

    • 那么我会在这里做什么?另外,有什么办法我不能使用“这个”,因为我从来没有,所以我真的不知道它是什么意思..?或者您知道编写此方法的更简单方法吗?我试图将其保留为一种方法,但我愿意使用一个类。谢谢! public void actionPerformed(ActionEvent actionEvent) { instruct.addActionListener(new ActionListener()); System.out.println(actionEvent.getActionCommand()); }
    • 抱歉,我修正了第一个解决方案。 ActionListeners 绑定到 Swing GUI 组件。 ActionListener 的每个实例都有自己的 actionPerformed() 方法,当操作触发该组件时会调用该方法。通过在您的ticTacToe 类中实现ActionListener,您可以保证actionPerformed 是您的类的一部分。当您说addActionlistener(this) 时,您是在要求要添加的组件在Menu 类的当前实例中调用actionPerformed()
    • 好的,所以我需要更改我的班级名称?或者我可以在方法中将instruct.addActionListener(new ActionListener()); 更改为instruct.addActionListener(this); 吗?
    • 为了让它运行,你必须把它改成第二个,但这没有任何意义,而且你很可能不会得到你想要的结果
    • 如果您了解什么是多态性和接口,那就更有意义了。多态性:stackoverflow.com/questions/10985223/java-polymorphism 接口:stackoverflow.com/questions/11389137/…
    猜你喜欢
    • 2014-03-16
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多