Hi i am using netbeans to make a tictactoe game so far this is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TicTacToe implements ActionListener
{
    //Class constants

    private static final int WINDOW_WIDTH = 400;
    private static final int WINDOW_HEIGHT = 400;
    private static final int TEXT_WIDTH = 10;

    //Design
    private static final GridLayout Layout_Style = new GridLayout(3,3);


    //Instance variables
    private JFrame window = new JFrame("TicTacToe");
    private JButton Square1 = new JButton("");
    private JButton Square2 = new JButton("");
    private JButton Square3 = new JButton("");
    private JButton Square4 = new JButton("");
    private JButton Square5 = new JButton("");
    private JButton Square6 = new JButton("");
    private JButton Square7 = new JButton("");
    private JButton Square8 = new JButton("");
    private JButton Square9 = new JButton("");

    String mark = "X";
    boolean win = false;
    Color black = new Color(0,0,0);
    Color notblack = new Color(0,0,0);
    //Constructor

    public TicTacToe()
    {

        window.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// exits the application one the game has been won

        //Add to window the internet helped me to see what this should look like
        window.getContentPane().setLayout(Layout_Style);
        window.getContentPane().add(Square1);
        window.getContentPane().add(Square2);
        window.getContentPane().add(Square3);
        window.getContentPane().add(Square4);
        window.getContentPane().add(Square5);
        window.getContentPane().add(Square6);
        window.getContentPane().add(Square7);
        window.getContentPane().add(Square8);
        window.getContentPane().add(Square9);
        //Add listener
        Square1.addActionListener(this);
        Square2.addActionListener(this);
        Square3.addActionListener(this);
        Square4.addActionListener(this);
        Square5.addActionListener(this);
        Square6.addActionListener(this);
        Square7.addActionListener(this);
        Square8.addActionListener(this);
        Square9.addActionListener(this);

        window.setVisible(true);
    }
        //ActionPerformed
    public void actionPerformed(ActionEvent e)
    {
        int count=1;
    do
    {

    if (e.getSource()==Square1)
//If user marks Square 1 then it will set a mark or it will continue on searching until a Square is marked
    {
        Square1.setText(mark);
        Square1.setEnabled(false);
    }
    else if (e.getSource()==Square2)
    {
        Square2.setText(mark);
        Square2.setEnabled(false);
    }
    else if (e.getSource()==Square3)
    {
        Square3.setText(mark);
        Square3.setEnabled(false);
    }
    else if (e.getSource()==Square4)
    {
        Square4.setText(mark);
        Square4.setEnabled(false);
    }
    else if (e.getSource()==Square5)
    {
        Square5.setText(mark);
        Square5.setEnabled(false);
    }
    else if (e.getSource()==Square6)
    {
        Square6.setText(mark);
        Square6.setEnabled(false);
    }
    else if (e.getSource()==Square7)
    {
        Square7.setText(mark);
        Square7.setEnabled(false);
    }
    else if (e.getSource()==Square8)
    {
        Square8.setText(mark);
        Square8.setEnabled(false);
    }
    else if (e.getSource()==Square9)
    {
        Square9.setText(mark);
        Square9.setEnabled(false);
    }

    //Checks to see if 3 corresponding X's or O's Match-up
    if (Square1.getText().equals(Square2.getText()) && Square2.getText().equals
            (Square3.getText()) && Square1.getText().equals("")==false)
    {
        Square1.setBackground(notblack);
        Square2.setBackground(notblack);
        Square3.setBackground(notblack);
        win=true;
    }
    else if (Square4.getText().equals(Square5.getText()) && Square5.getText().equals
            (Square6.getText())&& Square4.getText().equals("")==false)
    {
        Square4.setBackground(notblack);
        Square5.setBackground(notblack);
        Square6.setBackground(notblack);
        win=true;
    }
    else if (Square7.getText().equals(Square8.getText()) && Square8.getText().equals
            (Square9.getText())&& Square7.getText().equals("")==false)
    {
        Square7.setBackground(notblack);
        Square8.setBackground(notblack);
        Square9.setBackground(notblack);
        win=true;
    }
    else if (Square1.getText().equals(Square4.getText()) && Square4.getText().equals
            (Square7.getText())&& Square1.getText().equals("")==false)
    {
        Square1.setBackground(notblack);
        Square4.setBackground(notblack);
        Square7.setBackground(notblack);
        win=true;
    }
    else if (Square2.getText().equals(Square5.getText()) && Square5.getText().equals
            (Square8.getText())&& Square2.getText().equals("")==false)
    {
        Square2.setBackground(notblack);
        Square5.setBackground(notblack);
        Square8.setBackground(notblack);
        win=true;
    }
    else if (Square3.getText().equals(Square6.getText()) && Square6.getText().equals
            (Square9.getText())&& Square3.getText().equals("")==false)
    {
        Square3.setBackground(notblack);
        Square6.setBackground(notblack);
        Square9.setBackground(notblack);
        win=true;
    }
    else if (Square1.getText().equals(Square5.getText()) && Square5.getText().equals
            (Square9.getText())&& Square1.getText().equals("")==false)
    {
        Square1.setBackground(notblack);
        Square5.setBackground(notblack);
        Square9.setBackground(notblack);
        win=true;
    }
    else if (Square3.getText().equals(Square5.getText()) && Square5.getText().equals
            (Square7.getText())&& Square3.getText().equals("")==false)
    {
        Square3.setBackground(notblack);
        Square5.setBackground(notblack);
        Square7.setBackground(notblack);
        win=true;
    }

     if (count==9 && win== false)
    {
        JOptionPane.showMessageDialog(null, mark + "Too bad its a tie");//at the moment this isn't working but i cannot see why
        System.exit(1);
        win=false;
    }

    System.out.println("Button Pressed");
    if (count!=9 && win==true)
    {
        JOptionPane.showMessageDialog(null, mark + " You Win!!!");
        System.exit(1);
    }
        if (mark.equals("X"))
    {
        mark="O";
    }
    else
    {
        mark="X";
    }

    }

        while(win=false);

        }
public static void main(String[] args)

    {
    TicTacToe gui = new TicTacToe();


}
}

i am wanting three things
1. when the application is run it appears in the center of the screen.
2. the buttons are a colour apart from gray
3. the x's and o's are both different colours ect x is blue and o is green

If you can help me, i would be very much appreciated!

Edited 3 Years Ago by Reverend Jim: Fixed formatting

also the tie button won't work, when its a tie nothing happens- any ideas as to why?
Also if possible i need help to this question ASAP

I've managed to get the window to appear in the center so no longer need help with this.. Still trying real hard to get the colour to work however

I didn't look through all of it very carefully, but perhaps you want to define "notblack" to be a color other than black?

Color notblack = new Color(0,0,0);

Edited 7 Years Ago by Ezzaral: n/a

I didn't look through all of it very carefully, but perhaps you want to define "notblack" to be a color other than black?

Color notblack = new Color(0,0,0);

I really wish it was this simple but i changed it to a colour and it made no difference what-so-ever which is highly annoying. Any other suggestions would be highly appreciated

Ive been told that this code:
button.setBackground(Color.YELLOW)
will change the colour of the button which i added to this part of the code
Square1.setBackground(Color.YELLOW);
Square2.setBackground(Color.YELLOW);
Square3.setBackground(Color.YELLOW);
win=true;
}

But nothing seems to have changed- is there anything else that i might need to add to make this work?

It worked just fine for me. I ran your code and the button backgrounds change to show the winning sequence.

Hi ive been working for a few days now trying to figure out how to make it so when a button is pressed in this game instead of just a simple X or O shows up an image of lets say a dog for X and a cat for O or something along these lines- i cannot figure it out so would any one be able to help me?

This article has been dead for over six months. Start a new discussion instead.