Page 1 of 3 123 LastLast
Results 1 to 20 of 60
  1. #1
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default Menu Help in TicTacToe Game

    Code works fine..but when i scroll down on the menu bar and click "exit" it doesnt do anything. Any help would be much appreciated.


    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 extends Frame 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 TextField lcd;
        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);
    
    
        lcd = new TextField(10);
    	lcd.setEditable(false);
        add(lcd, BorderLayout.SOUTH);
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    	/*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*/
    
    
    }
        public static void main(String[] args){
            TicTacToe starter = new TicTacToe();
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    when i scroll down on the menu bar and click "exit" it doesnt do anything

    What did you expect to happen? And what code was supposed to do that?

    -----

    I don't mean to suggest any particular implementation, but your choice of names is interesting. aboutAction etc suggests that you might be thinking of adding actions to the menu which would make sense: actions do things while menu items are little more than selectable labels. (think restaurant menu items vs meals)

    Actions and menus are well described in Oracle's Tutorial.

  3. #3
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    Well i tried just adding the "exit feature" but i get one Error! How can i fix this if i cant have 2 ".getActionCommand's?" what would i replace it with.

    F:\TicTacToe\TicTacToe5.java:93: cannot find symbol
    symbol : variable e
    location: class TicTacToe5
    String arg = e.getActionCommand();
    ^
    1 error

    Tool completed with exit code 1




    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 TicTacToe5 extends Frame 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 TextField lcd;
        private Choice colors= new Choice();
    
    
    /////////////////////////////////////////////////////////////////////////////////////////
    
        public TicTacToe5(){
    
    	/*Creates the menu bar*/
    
    	MenuBar mnuBar = new MenuBar();
    	setMenuBar(mnuBar);
    
    	Menu mnuFile = new Menu("File", true);
    	mnuBar.add(mnuFile);
    	MenuItem mnuFileExit = new MenuItem("Exit");
    	mnuFile.add(mnuFileExit);
    
    	mnuFileExit.addActionListener(this);
    
    	mnuFileExit.setActionCommand("Exit");
    
    
    
    
        lcd = new TextField(10);
    	lcd.setEditable(false);
        add(lcd, BorderLayout.SOUTH);
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    	/*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";
    }
    
    {
    
    		String arg = e.getActionCommand();
    		if (arg == "Exit")
    		System.exit(0);
    
    }
    
            /*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*/
    
    
    }
        public static void main(String[] args){
            TicTacToe starter = new TicTacToe();
        }
    }

  4. #4
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    ^^^^^^^^^^^^^anyone?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    F:\TicTacToe\TicTacToe5.java:93: cannot find symbol
    symbol : variable e

    The "cannot find symbol" message means that you have used a name (in this case e) and you have not declared it anywhere. Common causes are typos and, in the case of methods, getting the arguments wrong.

  6. #6
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    what can i do to change or fix it?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Well, it's a typo so you have to correct it.

    What did you mean the following line to do?

    Java Code:
    String arg = e.getActionCommand();

  8. #8
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    i thought it was somehow related with this line...

    Java Code:
    mnuFileExit.setActionCommand("Exit");

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

    Default

    Quote Originally Posted by Ryan10 View Post
    i thought it was somehow related with this line...

    Java Code:
    mnuFileExit.setActionCommand("Exit");
    The key is in the error message -- it can't find the variable "e", and when I look at your code, neither can I. So where is this "e" variable that you're trying to use? (hint, it doesn't exist -- you have to use something else).

  10. #10
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    Yeah, thats what i was going to say but what else could i use instead of the getAction command for it to work even.

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

    Default

    Quote Originally Posted by Ryan10 View Post
    Yeah, thats what i was going to say but what else could i use instead of the getAction command for it to work even.
    It's not the method that's the problem here, again it's the variable -- you're using "e" but where is there a variable or method parameter "e"? What variable should you be using instead? Think before answering.

  12. #12
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    if i change this line to "ActionEvent e"

    Java Code:
    public void actionPerformed(ActionEvent a) {
    Then it will mess this line up

    Java Code:
    JButton pressedButton = (JButton)a.getSource();

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do not use the variable "e". Use another variable - one that you have declared in the method.

    edit: It doesn't matter whether you use a, e or a meaningful name like event. But, whatever variable you choose, use it consistently throughout the method.

  14. #14
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    i need to change this

    Java Code:
    public void actionPerformed(ActionEvent a) {
    (ActionEvent "then what do i put here?")

  15. #15
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    When i change it to a.getActionCommand i dont get any errors but when i do try and exit it doesnt "exit" the screen. It just stays there.

    Java Code:
    String arg = a.getActionCommand();

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    When you compare strings (or any other objects) use equals(), not ==.

    Java Code:
    if(arg.equals("Exit")) {
        // etc
    }
    ------------------

    If it still won't exit, post the code you are using for this method now.

  17. #17
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    Didn't work
    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 TicTacToe5 extends Frame 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 TextField lcd;
        private Choice colors= new Choice();
    
    
    /////////////////////////////////////////////////////////////////////////////////////////
    
        public TicTacToe5(){
    
    	/*Creates the menu bar*/
    
    	MenuBar mnuBar = new MenuBar();
    	setMenuBar(mnuBar);
    
    	Menu mnuFile = new Menu("File", true);
    	mnuBar.add(mnuFile);
    	MenuItem mnuFileExit = new MenuItem("Exit");
    	mnuFile.add(mnuFileExit);
    
    	mnuFileExit.addActionListener(this);
    
    	mnuFileExit.setActionCommand("Exit");
    
    
    
    
        lcd = new TextField(10);
    	lcd.setEditable(false);
        add(lcd, BorderLayout.SOUTH);
    
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    	/*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";
    }
    
    {
    
    		String arg = a.getActionCommand();
    		if (arg.equals ("Exit")){
    		System.exit(0);
    
    
    }
    }
    
            /*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*/
    
    
    }
        public static void main(String[] args){
            TicTacToe starter = new TicTacToe();
        }
    }

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Be careful! Your class is TicTacToe5 but the main() method says

    Java Code:
    TicTacToe starter = new TicTacToe();

    (one way to detect this sort of problem is to put the version numbr in the frame's title)

  19. #19
    Ryan10 is offline Senior Member
    Join Date
    Mar 2011
    Posts
    144
    Rep Power
    0

    Default

    I changed it to TicTacToe5 and now the menu doesnt even show up.

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Ryan10 View Post
    I changed it to TicTacToe5 and now the menu doesnt even show up.

    Now it doesn't even show up? It was never showing up! The menu you saw was for a different class: TicTacToe. Perhaps you should go back to the code for that class and see how you made the menu appear.

    ------------------

    Now that I've looked at the constructor of your class... You are adding the menu to the TicTacToe5 instance but what you are displaying is a totally different frame (window).

Page 1 of 3 123 LastLast

Similar Threads

  1. TicTacToe game
    By pinotje in forum New To Java
    Replies: 9
    Last Post: 12-23-2010, 05:49 PM
  2. Tictactoe game
    By Fowler in forum New To Java
    Replies: 11
    Last Post: 10-31-2010, 09:55 PM
  3. I need feedback on my TicTacToe game
    By kiregad in forum New To Java
    Replies: 4
    Last Post: 03-21-2010, 10:09 PM
  4. Need help with a menu button in a game
    By pjr5043 in forum Java Applets
    Replies: 1
    Last Post: 12-07-2008, 09:30 PM
  5. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 11: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
  •