因此,我有一个向容器添加九个JButtons
的类,但是在实现按钮实际执行任何操作的方式时遇到了麻烦。我试图制作一个基本的Tic Tac Toe游戏,只需将X放在您单击的第一个按钮上,将O放在第二个按钮上,依此类推。我想我可以使用ActionListener
,但是由于我制作了自己的名为Interface的类,已经扩展了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的第一学期,因此我不确定自己在做什么。我的错误可能确实很明显,但是经过几个小时的搜索,我仍然不知道自己在做什么错。任何帮助将不胜感激。
谢谢。
最佳答案
ActionListener
在java.awt.event
包中。您将需要导入此软件包,因为导入java.awt.*
不包括子软件包。
import java.awt.event.*;