Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Tic Tac Toe Java Application Help w/ Importing Text Field

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe Java Application Help w/ Importing Text Field

    Here is the code I am currently working with and am having troubles starting with trying to get my results to end up in a SOUTH text field to display who wins. If anybody has any tips or hints please do help I need results to end up in a text field preferably incorporated below the tic tac toe game. I have inputted in now and a window pops up telling who wins, which i understand I just need it to show in a textfield.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import java.awt.Color;
     
     
     
    public class TicTacToe implements ActionListener {
        /*Instance Variables*/
        private int[][] winCombinations = new int[][] {
                {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
                {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
                {0, 4, 8}, {2, 4, 6}             //diagonal wins
     
            };
        private JFrame window = new JFrame("Tic-Tac-Toe");
        private JButton buttons[] = new JButton[9];
        private int count, xWins, oWins, X, O, SOUTH, b1 = 0;
        private String letter = "";
        private boolean win = false;
        private Choice colors= new Choice();
     
     
    /////////////////////////////////////////////////////////////////////////////////////////
     
        public TicTacToe(){
     
    	/*Creates the menu bar*/
     
    	JMenuBar menuBar = new JMenuBar();
        window.setJMenuBar(menuBar);
     
        /*Creates "File" Button to Menu*/
     
         JMenu fileMenu = new JMenu("File");
         menuBar.add(fileMenu);
     
     
    	/*Creats drop down action buttons to grid*/
     
         JMenuItem clearAction =   new JMenuItem("Clear");
    	 JMenuItem aboutAction =  new JMenuItem("About");
    	 JMenuItem exitAction =    new JMenuItem("Exit");
    	 fileMenu.add(clearAction);
    	 fileMenu.add(aboutAction);
    	 fileMenu.addSeparator();
    	 fileMenu.add(exitAction);
     
    ////////////////////////////////////////////////////////////////////////////////////////////
     
    	/*Create Window*/
     
        window.setSize(300,300);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new GridLayout(3,3));
     
    	/*Add Buttons To The Window*/
        for(int i=0; i<=8; i++){
     
            buttons[i] = new JButton();
            window.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
     
        /*Make The Window Visible*/
        window.setVisible(true);
        }
     
        /**
         When an object is clicked, perform an action.
         @param a action event object
         */
        public void actionPerformed(ActionEvent a) {
            count++;
     
            /*Calculate whose turn it is*/
            if(count % 2 == 0){
    			letter = "O";
            } else {
                letter = "X";
            }
     
     
     
     
            /*Write the letter to the button and deactivate it*/
             JButton pressedButton = (JButton)a.getSource();
             pressedButton.setText(letter);
             pressedButton.setEnabled(false);
             pressedButton.setBackground(Color.RED);
     
     
     
     
     
     
     
     
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     
     
            /*Determine who won*/
            for(int i=0; i<=7; i++){
                if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
                    buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
                    buttons[winCombinations[i][0]].getText() != ""){
                    win = true;
                }
            }
     
            /*Show a dialog when game is over*/
     
            if(win == true){
                JOptionPane.showMessageDialog(null, letter + " wins the game!");
                System.exit(0);
            } else if(count == 9 && win == false){
                JOptionPane.showMessageDialog(null, "The game was tie!");
    			playAgainDialog();
            }
        }
     
     
        public void playAgainDialog() {
    			if(letter.equals("X"))  xWins++;
    			else                    oWins++;
     
    			int response = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
     
    			if(response == JOptionPane.YES_OPTION)   reset();
    			else                                     System.exit(0);
     
    }
     
     
    	public void reset() {
    			for(int i=0; i<=8; i++){
    				buttons[i].setText("");
    				buttons[i].setEnabled(true);
    			}
    			win = false;
    			count = 0;
     
    }
     
        public static void main(String[] args){
            TicTacToe starter = new TicTacToe();
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field


  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    Basically what I am having problems with is this:

    I need this not to show up as a Dialog...but I need it to show up in a TextField of my Border Layout
    My current code for that:

       if(win == true){
                JOptionPane.showMessageDialog(null, letter + " wins the game!");
                System.exit(0);
            } else if(count == 9 && win == false){
                JOptionPane.showMessageDialog(null, "The game was tie!");
    			playAgainDialog();
            }
        }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    You are getting plenty of help in the other forum. Do not waste peoples time by asking for help here also.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    Nobody has really "helped" me yet they are giving me hints to what I need to fix. And still I am having troubles

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    SO?

    Keep it in one thread. If you ask for help here then someone who is not aware of the other thread may waste their time given you the same help that you have already received.

    The Problems With Cross Posting

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    Place a text field in the panel, and set it's visibility to false with the default text "You Won" and when this condition comes true, enable that field and you will get what you want.
    Hope it helps...

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    Quote Originally Posted by Mr.777 View Post
    Place a text field in the panel, and set it's visibility to false with the default text "You Won" and when this condition comes true, enable that field and you will get what you want.
    Hope it helps...
    Ok thanks so this is what I have come up with but no textfield has come up is there any hints you might have to help me get the condition to work so my results will show up? This is what I have for that last part of what you told me what I should add:

        public static void main(String[] args){
     
    	JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
     
         JTextField tf = new JTextField("You Won");
    	 f.add(tf, BorderLayout.SOUTH); //Changed
    	 f.pack();
         f.setVisible(false);

  9. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    Okay now it's not visible. Now in your if condition where you want it to display, set it's visibility to true.

  10. #10
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    Quote Originally Posted by Mr.777 View Post
    Okay now it's not visible. Now in your if condition where you want it to display, set it's visibility to true.
    Ok well right now I have in there a JOption message come up. I need to take that out and start and If else condition. How would I begin to start that?

  11. #11
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Tic Tac Toe Java Application Help w/ Importing Text Field

    if(yoursuccesscondition)
    {
    f.setvisible(true);
    }

Similar Threads

  1. Replies: 3
    Last Post: February 21st, 2011, 06:25 PM
  2. Replies: 1
    Last Post: January 12th, 2011, 04:55 AM
  3. Importing gif/pictures
    By javanerd in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2010, 11:58 PM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Replies: 0
    Last Post: December 3rd, 2009, 03:43 PM