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

    Default

    Thanks guys for all the help so far now i need to add one more feature and its an "about" feature that just show's my name in and class in a dialog box when clicked i have the basic outline of it but not sure it will work.



    Java Code:
    if (arg == "About")
    {
    String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
    JOptionPane.showMessageDialog(null,message,
    "About TicTacToe", JOptionPane.INFORMATION_MESSAGE);
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    I need to add this feature in somewhere !!



    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(){
    
    		JMenuBar menuBar = new JMenuBar();
    		window.setJMenuBar(menuBar);
    
    		JMenu fileMenu = new JMenu("File",true);
    		menuBar.add(fileMenu);
    
    
    	/*Creates the menu bar*/
    JMenuItem exitAction = new JMenuItem("Exit");
    fileMenu.add(exitAction);
    exitAction.addActionListener(this);
    exitAction.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 == ("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){
            TicTacToe5 starter = new TicTacToe5();
        }
    }

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

    Default

    Well go for it -- add the feature with our blessings. But if you do this, you don't want to compare Strings with == but rather the equals method.

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

    Default

    if i just put in "=" it doesnt work

  4. #24
    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
    if i just put in "=" it doesnt work
    No of course not, and if you reread my recommendation, I didn't tell you to use =, again as mentioned above, use the equals method:


    Java Code:
    if (myStringA.equals("foo")) {
    
    }

    or you could use the equalsIgnoreCase if desired.

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

    Default

    The "exit part of this works perfectly fine but the "about action" doesn't any clues?

    Java Code:
    public TicTacToe5(){
    
    		JMenuBar menuBar = new JMenuBar();
    		window.setJMenuBar(menuBar);
    
    		JMenu fileMenu = new JMenu("File",true);
    		menuBar.add(fileMenu);
    
    		JMenu mnuAbout = new JMenu("About", true);
    		menuBar.add(mnuAbout);
    
    /////////////////////////////////////////////////////////////////////////////
    
        //////////////About Menu///////////
    
    
    
    	JMenuItem aboutAction = new JMenuItem("About TicTacToe");
    	mnuAbout.add(aboutAction);
    	aboutAction.addActionListener(this);
    	aboutAction.setActionCommand("About");
    
    
    //////*Creates the Exit Meny bar*//////
    	JMenuItem exitAction = new JMenuItem("Exit");
    	fileMenu.add(exitAction);
    	exitAction.addActionListener(this);
            exitAction.setActionCommand("Exit");

    Java Code:
    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 == ("Exit")){
    		System.exit(0);
    
    
    		if (arg == "About")
    		{
    		String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
    		JOptionPane.showMessageDialog(null,message,
    		"About TicTacToe", JOptionPane.INFORMATION_MESSAGE);
    
    
    }
    }
    }

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

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

    Default

    it put this and still doesnt work

    Java Code:
    if (arg.equals ("About"))
    		{
    		String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
    		JOptionPane.showMessageDialog(null,message,
    		"About TicTacToe", JOptionPane.INFORMATION_MESSAGE);

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

    Default

    Tidy up your code:

    (1) The { go at the end of the line. They don't start a line.
    (2) The } match up with the line above that ended with the matching {
    (3) Use spaces to indent. (they appear better when you post code)

    I mention all this because it is crazy brackets that are stopping the message dialog from ever being shown.

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Why are you ignoring my recommendations??
    The OP also ignored it in a previous thread because 'it works' as it is. I gave up on it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    I click About and a bunch of stuff come up on the "black screen" . One line says-- javax.swing .JMenu Item cannot be cast to javax.swing.JButton

  11. #31
    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 JosAH View Post
    The OP also ignored it in a previous thread because 'it works' as it is. I gave up on it.
    With this behavior, he risks losing folks who would be willing to help him.



    Quote Originally Posted by Ryan10 View Post
    I click About and a bunch of stuff come up on the "black screen" . One line says-- javax.swing .JMenu Item cannot be cast to javax.swing.JButton
    You need to post the actual error message.

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

    Default

    it wont let me copy it because it is showing in the "black screen console" here is my whole code for you guys to see for yourselves to make it easier for both of us.

    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(){
    
    		JMenuBar menuBar = new JMenuBar();
    		window.setJMenuBar(menuBar);
    
    		JMenu fileMenu = new JMenu("File",true);
    		menuBar.add(fileMenu);
    
    		JMenu mnuAbout = new JMenu("About", true);
    		menuBar.add(mnuAbout);
    
    /////////////////////////////////////////////////////////////////////////////
    
        //////////////About Menu///////////
    
    
    
    	JMenuItem aboutAction = new JMenuItem("About TicTacToe");
    	mnuAbout.add(aboutAction);
    	aboutAction.addActionListener(this);
    	aboutAction.setActionCommand("About");
    
    
    //////*Creates the Exit Meny bar*//////
    	JMenuItem exitAction = new JMenuItem("Exit");
    	fileMenu.add(exitAction);
    	exitAction.addActionListener(this);
    	exitAction.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 == ("Exit")){
    		System.exit(0);
    
    
    if (arg == ("Clear")){
    
    }
    
    
    		if (arg.equals ("About")){
    		String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
    		JOptionPane.showMessageDialog(null,message,
    		"About TicTacToe", JOptionPane.INFORMATION_MESSAGE);
    
    
    }
    }
    }
    
            /*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){
            TicTacToe5 starter = new TicTacToe5();
        }
    }

  13. #33
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Analise and rewrite your actionPerformed method. You made a mess there...

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

    Default

    looked fine to me..dont know what else i would change about it.

  15. #35
    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
    looked fine to me..dont know what else i would change about it.
    Your indentations are poorly formatted making it hard for us to read your code and making it hard for you to debug it. Heck, I can't even tell where the method ends based on your indentations.

    Please tell me which is easier to read, which is easier to understand at a glance, this:

    Java Code:
        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 == ("Exit")){
    		System.exit(0);
    
    
    if (arg == ("Clear")){
    
    }
    
    
    		if (arg.equals ("About")){
    		String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
    		JOptionPane.showMessageDialog(null,message,
    		"About TicTacToe", JOptionPane.INFORMATION_MESSAGE);
    
    
    }
    }
    }
    
            /*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*/
    
    
    }


    or this:

    Java Code:
       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 == ("Exit")) {
                System.exit(0);
    
                if (arg == ("Clear")) {
    
                }
    
                if (arg.equals("About")) {
                   String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
                   JOptionPane.showMessageDialog(null, message, "About TicTacToe",
                            JOptionPane.INFORMATION_MESSAGE);
                }
             }
          }
    
          /* 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;
             }
          }
       }
    And you still haven't changed the == to an equals method in several spots in this method, despite being told many times by many people to fix this.

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

    Default

    I have it in another code and it works fine..i didnt change it for the "exit" method and it still works but the "about method" doesnt. Doesnt make a difference if i change it or not it still doesnt work. Can't find out what the problem even is anyways

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

    Default

    There..

    Java Code:
    public void actionPerformed(ActionEvent e) {
            count++;
    
            /*Calculate whose turn it is*/
            if(count % 2 == 0){
    			letter = "O";
            } else {
                letter = "X";
            }
    
    		String arg = e.getActionCommand();{
    		if (arg.equals ("Exit")){
    		System.exit(0);
    
    
    if (arg == ("Clear")){
    
    }
    
    
    		if (arg.equals ("About")){
    		      String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
    		      JOptionPane.showMessageDialog(null,message,
    		      "About TicTacToe", JOptionPane.INFORMATION_MESSAGE);
    
    
     	  }
        }
     }
    
            /*Write the letter to the button and deactivate it*/
             JButton pressedButton = (JButton)e.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;
            }
         }
     }
    
        public static void main(String[] args){
            TicTacToe5 starter = new TicTacToe5();
        }
    }
    Last edited by Ryan10; 04-02-2011 at 03:30 PM.

  18. #38
    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
    There..
    indentations?

    It may seem picky, but if you want volunteers to give free help, the least you could do is help them be able to read the code. It really isn't asking too much of you is it?

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

    Default

    still wont help me in fixing the problem...you already have it indented for me so whats the point

  20. #40
    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
    still wont help me in fixing the problem...you already have it indented for me so whats the point
    The point is you still have to format your complete code, and you should give more details on your entire current problem since this doesn't tell us much of the details of the problem:

    it wont let me copy it because it is showing in the "black screen console" here is my whole code for you guys to see for yourselves to make it easier for both of us.

    but up to you. Good luck.

Page 2 of 3 FirstFirst 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
  •