有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing出现错误:无法实例化java类型。awt。事件监听器

所以我正在尝试做一个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());
      }
}

共 (0) 个答案