So I have a class that adds nine JButtons
to a container, but am having trouble implementing a way for the buttons to actually do anything. I'm trying to make a rudimentary Tic Tac Toe game the simply places an X on the first button you click, O on the second, etc. I figured I could use ActionListener
, but since I made my own class called Interface which already extends JFrame
, I figured I could implement ActionListener
. Doing this results in a "cannot find symbol" error on line 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);
}
}
This is still my first semester of Java so I'm not really sure what I'm doing. My mistake is probably really obvious, but after a couple hours of searching I still just don't know what I'm doing wrong. Any help would be very greatly appreciated.
Thanks.
ActionListener
is in thejava.awt.event
package. You will need to import this package as importingjava.awt.*
doesn't include sub-packages.