For our final project in our computer math class, we have to make a game, my group is doing tic tac toe
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
public class Driver extends JFrame
{
public static void main(String[] args)
{
Driver gst = new Driver();
JFrame frame = new JFrame("Tic Tac Toe");
frame.setSize(1280, 720);
frame.setLocation(200, 100);
gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gst.pack();
gst.setVisible(true);
}
public Driver()
{
Container pane = getContentPane();
pane.setLayout(new GridLayout(3, 3));
pane.setPreferredSize(new Dimension(600, 600));
////////Buttons////////
//1,1
JButton oneOnebutton = new JButton();
pane.add(oneOnebutton);
oneOnebutton.addActionListener(new oneOneListener());
oneOnebutton.setBackground(Color.WHITE);
//1,2
JButton oneTwobutton = new JButton();
pane.add(oneTwobutton);
oneTwobutton.addActionListener(new oneTwoListener());
oneTwobutton.setBackground(Color.WHITE);
//1,3
JButton oneThreebutton = new JButton();
pane.add(oneThreebutton);
oneThreebutton.addActionListener(new oneThreeListener());
oneThreebutton.setBackground(Color.WHITE);
//2,1
JButton twoOnebutton = new JButton();
pane.add(twoOnebutton);
twoOnebutton.addActionListener(new twoOneListener());
twoOnebutton.setBackground(Color.WHITE);
//2,2
JButton twoTwobutton = new JButton();
pane.add(twoTwobutton);
twoTwobutton.addActionListener(new twoTwoListener());
twoTwobutton.setBackground(Color.WHITE);
//2,3
JButton twoThreebutton = new JButton();
pane.add(twoThreebutton);
twoThreebutton.addActionListener(new twoThreeListener());
twoThreebutton.setBackground(Color.WHITE);
//3,1
JButton threeOnebutton = new JButton();
pane.add(threeOnebutton);
threeOnebutton.addActionListener(new threeOneListener());
threeOnebutton.setBackground(Color.WHITE);
//3,2
JButton threeTwobutton = new JButton();
pane.add(threeTwobutton);
threeTwobutton.addActionListener(new threeTwoListener());
threeTwobutton.setBackground(Color.WHITE);
//3,3
JButton threeThreebutton = new JButton();
pane.add(threeThreebutton);
threeThreebutton.addActionListener(new threeThreeListener());
threeThreebutton.setBackground(Color.WHITE);
}
int turn = 1;
private class oneOneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
oneOnebutton.setText("x");
}
}
//******1,2******//
private class oneTwoListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******1,3*********//
private class oneThreeListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******2,1*********//
private class twoOneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******2,2*********//
private class twoTwoListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******2,3*********//
private class twoThreeListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******3,1*********//
private class threeOneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******3,2*********//
private class threeTwoListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
//******3,3*********//
private class threeThreeListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
If you scroll down to action listeners, I have an action listener setup for the button named oneOne (1,1 on the 3x3 grid i have) It gives me an error that says that points to oneOne and says cannot find symbol, what could I do to fix this?
oneOnebutton
is defined as a local variable in the Constructor
of Dirver
, you can not access it out of the Constructor
.
You can make a class member of Dirver
.
public class Driver extends JFrame {
JButton oneOnebutton;
....
}
and initiate in the Constructor
:
public Driver() {
....
oneOnebutton = new JButton();
....
}
this can make sure oneOnebutton
is available in oneOneListener
.