1 Replies - 2946 Views - Last Post: 22 December 2010 - 03:35 PM Rate Topic: -----

#1 Guest_Brandon*


Reputation:

Java Swing Tic tac toe

Posted 22 December 2010 - 11:24 AM

Im making a tic tac toe game with java swing, and it went pretty good up to the end bit with the reveal button method. Im trying to make is so when the user clicks the first time on the console, an x will show up, and the second time they click an O will show up. Anyone know a good way to implament this into my program?


// The "Template" class.

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


public class ButtonReveal extends JApplet implements ActionListener
{
    //Create all labels, textfields and buttons needed (in order)

    JButton firstButton;
    JButton secondButton;
    JButton thirdButton;    
    JButton fourthButton;
    JButton fithButton;
    JButton sixthButton;
    JButton seventhButton;
    JButton eighthButton;
    JButton ninthButton;
    

    //Create any global variables/arrays needed later in the program (final)
    Container pane = getContentPane ();

    // Sets up GUI components.
    public void init ()
    {
        

        pane.setLayout (new GridLayout (3,3));

        setSize (400, 200);
        
        //Initialize and add buttons to pane
        firstButton = new JButton ();
        firstButton.addActionListener (this); //adds an actionListener to the button called "this"

        secondButton = new JButton ();
        secondButton.addActionListener (this);
        
        thirdButton = new JButton ();
        thirdButton.addActionListener (this);
        
        fourthButton = new JButton ();
        fourthButton.addActionListener (this);
        
        fithButton = new JButton ();
        fithButton.addActionListener (this);
        
        sixthButton = new JButton ();
        sixthButton.addActionListener (this);
        
        seventhButton = new JButton ();
        seventhButton.addActionListener (this);
        
        eighthButton = new JButton ();
        eighthButton.addActionListener (this);
        
        ninthButton = new JButton ();
        ninthButton.addActionListener (this);

        pane.add (firstButton, 0); //second parameter is the index on pane
        pane.add (secondButton, 1);
        pane.add (thirdButton, 2);
        pane.add (fourthButton, 3);
        pane.add (fithButton, 4);
        pane.add (sixthButton, 5);
        pane.add (seventhButton, 6);
        pane.add (eighthButton, 7);
        pane.add (ninthButton, 8);


        setContentPane (pane);
    } // init method


    // Respond to action of user.
    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () instanceof JButton)
        {
            if (e.getSource () == firstButton)
                revealButton (firstButton, 0);
            else if (e.getSource () == secondButton)
                revealButton (secondButton, 1);
            else if (e.getSource () == thirdButton)
                revealButton (thirdButton, 2);
            else if (e.getSource () == fourthButton)
                revealButton (fourthButton, 3);
            else if (e.getSource () == fithButton)
                revealButton (fithButton, 4);
            else if (e.getSource () == sixthButton)
                revealButton (sixthButton, 5);
            else if (e.getSource () == seventhButton)
                revealButton (seventhButton, 6);
            else if (e.getSource () == eighthButton)
                revealButton (eighthButton, 7);
            else if (e.getSource () == ninthButton)
                revealButton (ninthButton, 8);
               
                

        }
    } // actionPerformed method





    // respond to button pushed1
    public void revealButton (JButton button, int index)
    {

        
        //removes the indicated button from pane
        pane.remove(button);

        //creates a new text field to take the place of the button, makes it uneditable and sets background colour
        JTextField text = new JTextField();
        text.setEditable(false);
        text.setBackground(Color.WHITE);
        
        //sets text for textfield
        text.setText("X");
        
        //sets the alignment for the text in the field
        text.setHorizontalAlignment(text.CENTER);
        
        //adds the new textfield to the pane at the location of the old button
        pane.add(text, index);
        
        //re-creates pane with new information
        setContentPane(pane);
        
        
        


    } // calculateDay method
} /* Calendar class */





Is This A Good Question/Topic? 0

Replies To: Java Swing Tic tac toe

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Swing Tic tac toe

Posted 22 December 2010 - 03:35 PM

First no need to remove the button from the panel and replace it by a JTextField
Still use the button and disbled it

Just have a counter and use % 2 on it for X or O

String[] symbol = {"X", "O"};
int turn = 0;

void revealButton(JButton button, ...) {
turn++;
button.setText(symbol[turn % 2]);
button.setEnabled(false);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1