Results 1 to 20 of 60
Thread: Menu Help in TicTacToe Game
- 03-31-2011, 08:18 PM #1
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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(); } }
- 03-31-2011, 09:05 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 04-01-2011, 05:40 PM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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(); } }
- 04-01-2011, 07:44 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
^^^^^^^^^^^^^anyone?
- 04-01-2011, 10:00 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 04-02-2011, 03:39 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
what can i do to change or fix it?
- 04-02-2011, 03:47 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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();
- 04-02-2011, 04:17 AM #8
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
i thought it was somehow related with this line...
Java Code:mnuFileExit.setActionCommand("Exit");
-
- 04-02-2011, 04:21 AM #10
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
Yeah, thats what i was going to say but what else could i use instead of the getAction command for it to work even.
-
- 04-02-2011, 04:27 AM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
if i change this line to "ActionEvent e"
Then it will mess this line upJava Code:public void actionPerformed(ActionEvent a) {
Java Code:JButton pressedButton = (JButton)a.getSource();
- 04-02-2011, 04:28 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 04-02-2011, 04:33 AM #14
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
i need to change this
(ActionEvent "then what do i put here?")Java Code:public void actionPerformed(ActionEvent a) {
- 04-02-2011, 04:38 AM #15
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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();
- 04-02-2011, 04:45 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 04-02-2011, 05:00 AM #17
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
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(); } }
- 04-02-2011, 05:12 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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)
- 04-02-2011, 05:14 AM #19
Senior Member
- Join Date
- Mar 2011
- Posts
- 144
- Rep Power
- 0
I changed it to TicTacToe5 and now the menu doesnt even show up.
- 04-02-2011, 05:27 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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).
Similar Threads
-
TicTacToe game
By pinotje in forum New To JavaReplies: 9Last Post: 12-23-2010, 05:49 PM -
Tictactoe game
By Fowler in forum New To JavaReplies: 11Last Post: 10-31-2010, 09:55 PM -
I need feedback on my TicTacToe game
By kiregad in forum New To JavaReplies: 4Last Post: 03-21-2010, 10:09 PM -
Need help with a menu button in a game
By pjr5043 in forum Java AppletsReplies: 1Last Post: 12-07-2008, 09:30 PM -
TicTacToe Game
By Ebtihal in forum New To JavaReplies: 0Last Post: 01-09-2008, 11:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks