在为同一类扩展和实现时遇到问题



所以我有一个类,它向容器中添加了九个JButtons,但是在实现按钮实际执行任何操作的方法时遇到了麻烦。 我正在尝试制作一个基本的井字游戏,只需在您单击的第一个按钮上放置一个 X,在第二个按钮上放置一个 O,依此类推。我想我可以使用ActionListener,但是由于我创建了自己的类,称为接口,它已经扩展了JFrame,我想我可以实现ActionListener。 这样做会导致第 4 行出现"找不到符号"错误。

import java.awt.*;
import javax.swing.*;
public class Interface extends JFrame implements ActionListener
{
    public Interface ()
    {
        super("Panel");
        //Creates the window
        Container c;
        c = getContentPane();
        c.setLayout(new GridLayout(3,3, 5, 5));          
        //Creates the buttons  
        JButton tLeft = new JButton(" ");
        JButton tMiddle = new JButton(" ");
        JButton tRight = new JButton(" ");
        JButton mLeft = new JButton(" ");
        JButton mMiddle = new JButton(" ");
        JButton mRight = new JButton(" ");
        JButton bLeft = new JButton(" ");
        JButton bMiddle = new JButton(" ");
        JButton bRight = new JButton(" ");
        c.add(tLeft);
        c.add(tMiddle);
        c.add(tRight);
        c.add(mLeft);
        c.add(mMiddle);
        c.add(mRight);
        c.add(bLeft);
        c.add(bMiddle);
        c.add(bRight);

        setSize(250,250);
        setVisible(true);
    }
    public static void main(String[]args)
    {
      Interface Message=new Interface();
        Message.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

这仍然是我学习Java的第一学期,所以我不太确定自己在做什么。 我的错误可能非常明显,但经过几个小时的搜索,我仍然不知道我做错了什么。 任何帮助将不胜感激。

谢谢。

ActionListenerjava.awt.event包中。您需要导入此包,因为导入java.awt.*不包括子包。

import java.awt.event.*;

最新更新