I'm trying to write a tic tac toe applet. Below is the code. I can't get it to line up in 3 rows of 3 and also it shows "win" as soon as 1 button is clicked. Any help would be appreciated. Thanks.

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class TicTacToe extends JApplet implements ActionListener 
{
    Container con = getContentPane();
    JLabel ticTacToe = new JLabel("Tic Tac Toe");
    JPanel displayPanel = new JPanel(new GridLayout(3,3));
    JButton button1 = new JButton(" ");
    JButton button2 = new JButton(" ");
    JButton button3 = new JButton(" ");
    JButton button4 = new JButton(" ");
    JButton button5 = new JButton(" ");
    JButton button6 = new JButton(" ");
    JButton button7 = new JButton(" ");
    JButton button8 = new JButton(" ");
    JButton button9 = new JButton(" ");
    int count;
    String letter;
    boolean win;
    public void init()
    {
        con.setLayout (new FlowLayout());
        //add buttons to the container
        con.add(ticTacToe);
        con.add(button1);
        con.add(button2);
        con.add(button3);
        con.add(button4);
        con.add(button5);
        con.add(button6);
        con.add(button7);
        con.add(button8);
        con.add(button9);
        //Add The Action Listener To The Buttons
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        button4.addActionListener(this);
        button5.addActionListener(this);
        button6.addActionListener(this);
        button7.addActionListener(this);
        button8.addActionListener(this);
        button9.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    { 
        count++; 
        //Decide who's turn it is
        if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
            { 
                letter = "X";
            } 
        else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
            { 
            letter = "O"; 
            } 
        /*Display X's or O's on the buttons*/ 
        if(e.getSource() == button1)
            { 
                button1.setText(letter); 
                button1.setEnabled(false);
            } 
        else if(e.getSource() == button2)
            { 
                button2.setText(letter);
            } 
        else if(e.getSource() == button3)
            { 
                button3.setText(letter); 
            } 
        else if(e.getSource() == button4)
            { 
                button4.setText(letter); 
            } 
        else if(e.getSource() == button5)
            { 
                button5.setText(letter); 
            } 
        else if(e.getSource() == button6)
            { 
                button6.setText(letter); 
            } 
        else if(e.getSource() == button7)
            { 
                button7.setText(letter); 
            } 
        else if(e.getSource() == button8)
            { 
                button8.setText(letter); 
            } 
        else if(e.getSource() == button9)
            { 
                button9.setText(letter); 
            }
        if( button1.getText() == button2.getText() 
            && button2.getText() == button3.getText() 
            && button1.getText() != "")
            {
                win = true;
            }
        else if(button4.getText() == button5.getText() 
            && button5.getText() == button6.getText() 
            && button4.getText() != "")
            {
                win = true;
            }
        else if(button7.getText() == button8.getText() 
            && button8.getText() == button9.getText() 
            && button7.getText() != "")
            {
                win = true;
            }
        //vertical wins
        else if(button1.getText() == button4.getText() 
            && button4.getText() == button7.getText() && button1.getText() != "")
            {
                win = true;
            }
        else if(button2.getText() == button5.getText() 
            && button5.getText() == button8.getText() && button2.getText() != "")
            {
                win = true;
            }
        else if(button3.getText() == button6.getText() 
            && button6.getText() == button9.getText() && button3.getText() != "")
            {
                win = true;
            }
        //diagonal wins
        else if(button1.getText() == button5.getText() 
            && button5.getText() == button9.getText() && button1.getText() != "")
            {
                win = true;
            }
        else if(button3.getText() == button5.getText() 
            && button5.getText() == button7.getText() && button3.getText() != "")
            {
                win = true;
            }
        else 
            {
                win = false;
            }
        if(win == true)
            { 
            JOptionPane.showMessageDialog(null, letter + "wins"); 
            } 
        else if(count == 9 && win == false)
            { 
            JOptionPane.showMessageDialog(null, "Tie Game!"); 
            }

    } 
}
Salem commented: Code tags = false => orange cookie = true -7

1# The problem is that you only have one panel and it's a flowlayout which will make components be added to the right and wrap to the next line if there's not enough room.

You should make the contentPane a BoxLayout and set it so that components are added vertically. eg. con.setLayout (new BoxLayout(con, BoxLayout.Y_AXIS)); so that you can add rows vertically. (OR you could set it con.setLayout (new BoxLayout(con, BoxLayout.X_AXIS)); if you decide to group the subpanels into columns.)

You should also have separate panels to grouping each row together (or you could group by columns) into one panel called BoxLayout e.g row1.setLayout(new BoxLayout(row1, BoxLayout.X_AXIS));
and setting BoxLayout.X_AXIS will add each component across.

Then you add the subpanels in the con.

See below what i did and try to understand it:

con.setLayout (new BoxLayout(con, BoxLayout.Y_AXIS)); //adds components below vertically
//add buttons to the container
con.add(ticTacToe);
JPanel row1 = new JPanel(); //subpanel1
...
row1.setLayout(new BoxLayout(row1, BoxLayout.X_AXIS)); //adds components across
...
row1.add(button1);
...
row2.add(button4);
...
row3.add(button7);
...
con.add(row1); //add subpanels to contentPane.
...

also try to print button1.getText() and you may see why they are saying win all the time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.