Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    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.

    Java Code:
    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
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    nobody can help :(

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you understand the basics of using a text field and how to set the data in them?

    Java Code:
    JTextField winnerText = new JTextField();
    winnerText.setEditable(false);
    winnerText.setText("Some text");
    This is the code to create a field, set it to not be editable, and then set the text being displayed.

    Check out the API for JTextField to find out more information.

  4. #4
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Not familiar with textfields so much I just need to figure out how to set it so that the results show in the textfield in the SOUTH of the border layout.

    Do you know what I'm asking?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    After creating the text field you can add it to a border layout frame with something like this
    Java Code:
    frame.add(component, BorderLayout.SOUTH);
    you can also do north, east, west and center.
    I suggest you check out some tutorials on jtextfield.

  6. #6
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    If I understand your question, you need to read up on layout managers. You are currently using a GridLayout with 9 cells and have addded 9 components. You probably want the tic tac toe cells on a separate JPanel with GridLayout. Then set the JFrame to different Layout Manger like BorderLayout. Then you can add components to the appropriate areas. There are many ways to do this but a good study of Layout Managers in the docs and tutorials would be a huge benefit to you.
    Last edited by yellowledbet; 03-29-2011 at 04:12 AM.

  7. #7
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Any suggested sites, in which I can have the results show up in the Text Field?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Big Bundy View Post
    Any suggested sites, in which I can have the results show up in the Text Field?
    The Swing tutorials will show you how to use JTextFields, but simply calling setText(String text) on the text field will suffice.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Good evening Darryl!

  11. #11
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    This is seeming more difficult then it really is. Can anybody give me an example of just getting a simple textfield to show up in my tictactoe program. Then from there how to get my results of "X" wins or "O" wins in that textfield.

    Would I have to add something like this to get started?

    window.setLayout(new BorderLayout(1,1));

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have already been given all the information you need.

    Add text to a textfield with the setText method.

    Add a textfield to the frame/panel by adding it with an appropriate layout manager.

    You were even provided a link to a very good tutorial. Now the onus is upon you to learn how to complete your task.

  14. #14
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok so here I've quickly made a sample BorderLayout with the textfield being in at the South end. Will this help in wondering where I may add this in to my own code so it works with what I have?

    Java Code:
    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    
    public class TextFieldTest {
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setLayout(new BorderLayout());
            JTextField tf = new JTextField();
            f.getContentPane().add(BorderLayout.SOUTH, tf);
            f.pack();
            f.setVisible(true);
        }
    }

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please try it on your program and see. You will probably learn more if you experiment with your code more and post here less. Seriously. The best way to learn this stuff is to play with your code, modify it, see what happens, find an error, try to fix the error, then come here when all else fails.

  16. #16
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Yes, I have inputed this in my code everything is successful, but when the game shows there is no border textfield?

  17. #17
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    You need to make it all work together. Below I added an example to your code that shows how to add an additional JPanel to a JFrame. This allows you to nest Layout Managers and have more control over your layout. This doesn't solve your problem and you will need to implement these concepts into your code but it shows how you can add the flexibility needed to accomplish your task.

    Java Code:
    import java.awt.*;//Changed for example
    
    import javax.swing.*;//Changed for example
    import javax.swing.JTextField;
    
    
    public class TextFieldTest {
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setLayout(new BorderLayout());
            /*******************************************************
             * Show how to add additional panel
             */
            JPanel panel = new JPanel(new GridLayout(3,3));
            JTextField[] textFieldArr = new JTextField[9];
            for(int i= 0; i < 9; i++){
                textFieldArr[i] = new JTextField("TextField: " + i);
                panel.add(textFieldArr[i]);
            }
    
            f.add(panel, BorderLayout.CENTER);
           
            //**********************************************************
    
    
             
            JTextField tf = new JTextField("");
            f.add(tf, BorderLayout.SOUTH); //Changed
            f.pack();
            f.setVisible(true);
        }
    }

  18. #18
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    I see how it has 9 boxes such for the GridLayout and the Textfield at the bottom, really nice..Now I need to replace the TextField :0 - TextField :8 with a real Grid Layout with X's and O's like I had before...why is it Text like that?

  19. #19
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Do you understand the basics of using a text field and how to set the data in them?

    Java Code:
    JTextField winnerText = new JTextField();
    winnerText.setEditable(false);
    winnerText.setText("Some text");
    This is the code to create a field, set it to not be editable, and then set the text being displayed.

    Check out the API for JTextField to find out more information.

    Ok....then I have to set it so when the condition is true then the results show? How would i start that?

  20. #20
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Still needed help with the textfield in which my results will show in..

Page 1 of 2 12 LastLast

Similar Threads

  1. Importing text data
    By gsashwin in forum New To Java
    Replies: 1
    Last Post: 06-21-2010, 08:16 PM
  2. Replies: 1
    Last Post: 04-26-2010, 02:02 PM
  3. Importing text into a word document
    By Frys82 in forum AWT / Swing
    Replies: 5
    Last Post: 08-24-2009, 09:41 PM
  4. Importing excel sheets into java application
    By blizzard in forum New To Java
    Replies: 1
    Last Post: 04-02-2009, 10:59 PM
  5. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-06-2008, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •