sorry for the wait.
CODE
//The "TicTacToe" class.
import java.awt.*;
//import hsa.Console;
import javax.swing.*;
import java.awt.event.*;
/*
*/
public class TicTacToe extends JFrame implements ActionListener // inputting an action listener for the buttons
{
private String letter = "";
private int count = 0;
private boolean win = false;
private JButton[] [] button = new JButton [3] [3];
JPanel TicPane;
Container contentPane;
public void actionPerformed (ActionEvent e)
{
String event;
event = e.getActionCommand ();
if (event.equals ("PRESS TO PLAY TIC TAC TOE"))
{
TicTacToee ();
return;
}
if (event.equals ("About"))
{
about ();
return;
}
if (event.equals ("Restart")) //this else if statements runs when the button named "Restart" is clicked on
{
//stop (choice); // this line stops the music from playing
hide (); // this line willhide the output window
TicTacToe window = new TicTacToe (); // creating a windown called GasStation
window.setTitle ("Welcome to the TicTacToe Game"); // Giving the window a title
window.setSize (900, 700); // giving the size of the window
window.setVisible (true); // asking if this true
}
// so it is a button
count++;
if (count % 2 == 1)
letter = "X";
else
letter = "O";
JButton b = (JButton) e.getSource ();
b.setText (letter);
b.setFont (new Font ("Times New Activate", Font.BOLD, 60));
win = true;
b.setEnabled (false);
win = false;
//horizontal wins
for (int i = 0; i < 3; i++)
{
if (button [i] [0].getText ().equals (button [i] [1].getText ()) && button [i] [0].equals (button [i] [2]))
win = true;
}
//virticle wins
for (int i = 0; i < 3; i++)
{
if (button [0] [i].getText ().equals (button [1] [i].getText ()) && button [0] [i].equals (button [2] [i]))
win = true;
}
//diagonal wins
if (button [0] [0].getText ().equals (button [1] [1].getText ()) && button [2] [0].getText ().equals (button [1] [1].getText ()))
;
win = true;
if (button [2] [0].getText ().equals (button [1] [1].getText ()) && button [0] [2].getText ().equals (button [1] [1].getText ()))
;
win = true;
if (win==true)
{
JOptionPane.showMessageDialog (null, letter + " WINS!");
}
else if (count == 10 && win == false)
{
JOptionPane.showMessageDialog (null, "Tie Game!");
}
}
public void TicTacToee ()
{
Frame frame = new Frame ("Button Frame");
frame.setLayout (null);
int k = 1;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
button [i] [j] = new JButton ("" + k);
k++;
button [i] [j].addActionListener (this);
button [i] [j].setBackground (Color.white);
button [i] [j].setSize (150, 150);
button [i] [j].setLocation (i * 150 + 5, j * 150 + 30);
frame.add (button [i] [j]);
}
}
frame.setSize (460, 485);
frame.setVisible (true);
frame.addWindowListener (new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
);
}
public void about ()
{
JLabel text, text1;
JButton restart;
if (TicPane != null) // this if statement runs if there is something on the JPanel
{
contentPane.remove (TicPane); // if there it is it makes the JPanel blank
}
TicPane = new JPanel (); // getting a new JPanel
TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 300, 30)); // creating a layout, to organize the output screen
text = new JLabel (" This is a game designed by Rishi Patel which allows you to have a multi-player game of TicTacToe");
text1 = new JLabel ("Created June of 2008");
validate (); // this method sets everything up in the out put screen
contentPane = getContentPane (); // getting a new content pane
if (TicPane != null) // this if statement runs if there is something on the JPanel
{
contentPane.remove (TicPane); // if there it is it makes the JPanel blank
}
restart = new JButton ("Restart");
restart.addActionListener (this);
TicPane.setLayout (null);
restart.setSize (500, 500);
restart.setLocation (5, 30);
TicPane = new JPanel (); // getting a new JPanel
TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 600, 30));
TicPane.add (text);
TicPane.add (text1);
TicPane.add (restart);
contentPane.add (TicPane); // adding the JPanel to the content pane
validate ();
}
public TicTacToe ()
{ JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
menuBar = new JMenuBar ();
setJMenuBar (menuBar);
//Create file menu
menu = new JMenu ("File");
menuBar.add (menu);
menuItem = new JMenuItem ("Restart");
menuItem.addActionListener (this);
menu.add (menuItem);
menuItem = new JMenuItem ("Quit");
menuItem.addActionListener (this);
menu.add (menuItem);
//Create file menu
menu = new JMenu ("Help");
menuBar.add (menu);
//File Menu Items
menuItem = new JMenuItem ("About");
menuItem.addActionListener (this);
menu.add (menuItem);
if (TicPane != null) // this if statement runs if there is something on the JPanel
{
contentPane.remove (TicPane);
}
TicPane = new JPanel (); // getting a new JPanel
TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 20, 15)); // creating a layout, to organize the output screen
contentPane = getContentPane (); // getting the content pane
JButton activate; // Initializing all the buttons in this program
TicPane = new JPanel (); // making a new JPanel
TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 20, 10));
activate = new JButton ("PRESS TO PLAY TIC TAC TOE");
activate.addActionListener (this);
TicPane.add (activate);
contentPane.add (TicPane); // adding the JPanel to the content pane
validate ();
}
public static void main (String[] args)
{
TicTacToe window = new TicTacToe ();
window.setTitle ("Welcome to the TicTacToe Game"); // Giving the window a title
window.setSize (900, 700); // giving the size of the window
window.setVisible (true);
}
}