Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default TicTacToe errors

    I get
    "N:\profile\java\Game.java:12: Game is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
    public class Game extends Frame implements ActionListener
    ^
    1 error

    Tool completed with exit code 1"
    As my error. I need to run a TicTacToe game. Right now, only the layout is needed.
    /*
    Assignment 8: A Java Game of TicTacToe
    Filename: Game.java
    Purpose: To create a Game with a menu
    */

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    public class Game extends Frame implements ActionListener
    {

    //this keeps other programs from grabbing stuff from here
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton button1 = new JButton("");
    private JButton button2 = new JButton("");
    private JButton button3 = new JButton("");
    private JButton button4 = new JButton("");
    private JButton button5 = new JButton("");
    private JButton button6 = new JButton("");
    private JButton button7 = new JButton("");
    private JButton button8 = new JButton("");
    private JButton button9 = new JButton("");
    private String letter = "";
    private int count = 0;
    private boolean win = false;
    private Button keys[];
    private Panel keypad;
    private TextField lcd;
    private double op1;
    private boolean first;
    private boolean foundKey;
    private boolean clearText;
    private int lastOp;

    //the actual constructed part of the program.
    public Game (){
    /*Create Window*/
    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setLayout(new GridLayout(3,3));

    /*Add Buttons To The Window*/
    window.add(button1);
    window.add(button2);
    window.add(button3);
    window.add(button4);
    window.add(button5);
    window.add(button6);
    window.add(button7);
    window.add(button8);
    window.add(button9);

    //create an instance of the menu
    MenuBar mnuBar = new MenuBar();
    setMenuBar(mnuBar);

    //construct and populate the File Menu
    Menu mnuFile = new Menu("File", true);
    mnuBar.add(mnuFile);
    MenuItem mnuFileExit = new MenuItem("Exit");
    mnuFile.add(mnuFileExit);

    //construct and populate the Edit menu
    Menu mnuEdit = new Menu("Edit", true);
    mnuBar.add(mnuEdit);
    MenuItem mnuEditClear = new MenuItem("Clear");
    mnuEdit.add(mnuEditClear);
    mnuEdit.insertSeparator(1);
    MenuItem mnuEditCopy = new MenuItem("Copy");
    mnuEdit.add(mnuEditCopy);
    MenuItem mnuEditPaste = new MenuItem("Paste");
    mnuEdit.add(mnuEditPaste);

    //construct and populate the About menu
    Menu mnuAbout = new Menu("About", true);
    mnuBar.add(mnuAbout);
    MenuItem mnuAboutGame = new MenuItem("About Game");
    mnuAbout.add(mnuAboutGame);

    //add the ActionListener to each menu
    mnuFileExit.addActionListener(this);
    mnuEditClear.addActionListener(this);
    mnuEditCopy.addActionListener(this);
    mnuEditPaste.addActionListener(this);
    mnuAboutGame.addActionListener(this);

    //construct components and initialize beginning values
    lcd = new TextField(20);
    lcd.setEditable(false);
    keypad = new Panel();
    keys = new Button[9];
    first = true;
    op1 = 0.0;
    clearText = true;
    lastOp = 0;


    addWindowListener(
    new WindowAdapter()
    {

    public void windowClosing(WindowEvent e)
    {
    System.exit(0);

    }
    }
    );

    //Need to see the window
    window.setVisible(true);
    }
    public static void main(String args[])
    {
    //set frame properties
    Game f = new Game();
    f.setTitle("Game Application");
    f.setBounds(200,200,300,300);
    f.setVisible(true);

    //set image properties and add to frame
    Image icon = Toolkit.getDefaultToolkit().getImage("calcImage.gi f");
    f.setIconImage(icon);
    }
    }

    Pretty much I am getting 2 windows that pop up. 1 is the game, the other is my menu bars. How do I fix this?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You didn't implement actionPerformed(ActionEvent e) .

  3. #3
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    ook. how would I do that? I am very new to this and am so lost right now.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    When your class implements an interface you have to override all methods from the interface.

    In this case add the following method to your class:
    Java Code:
    public void actionPerformed(ActionEvent e){
    
    }

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    java.sun.com/docs/books/tutorial/java/IandI/createinterface.html

    This is a decent tutorial, I' m not allowed to post links, yet.

  6. #6
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    and that can go anywhere?

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    anywhere outside other methods/constructors. Quick suggestion: you may want to extend JFrame instead of Frame. Then, instead of a WindowListener, you can simply use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    this is still my problem though. As you can see, I have 2 separate windows. I would like to get my menu bar on the same window as my game.


    btw, the actionperformed thing did work. tyvm.@!
    Attached Thumbnails Attached Thumbnails TicTacToe errors-error.jpg  

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

    Default

    You are creating two windows here, one is the current Game class object which extends Frame, and which you initialize in your main method as the "f" object, the other is a Window object which you call "window" and which you initialize in your Game constructor. So because you are creating and initializing two windows, you'll see two windows.

    Solution:
    1) First and foremost, Get rid of the Window object called window, and create only one root container or window, the main Game class, and add your menu bar to it in its constructor.
    2) Don't try to mix AWT and Swing components as you are doing as this is a recipe for disaster. I recommend that you use only Swing components here. Thus your Frame should be a JFrame. All Buttons should be JButtons. All TextFields should be JTextFields, all MenuBars should be JMenuBars, etc...

    You really need to go through the Sun Swing tutorials and study it, else this will be a painful process for you. Best of luck.

  10. #10
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    AHH. I'll try that tomorrow morning. tyvm. :D

  11. #11
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default new issue

    So I got the code working, now the only issue I have left is with my Clear button. I need my code to let the game board be cleared off when I hit Clear. My Exit command and my About command work, just not Clear.
    /*
    What: A Java Game of TicTacToe
    Filename: Game.java
    Purpose: To create a Game with a menu
    */

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    public class Game extends JFrame implements ActionListener

    {


    //this keeps other programs from grabbing stuff from here
    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton button1 = new JButton("");
    private JButton button2 = new JButton("");
    private JButton button3 = new JButton("");
    private JButton button4 = new JButton("");
    private JButton button5 = new JButton("");
    private JButton button6 = new JButton("");
    private JButton button7 = new JButton("");
    private JButton button8 = new JButton("");
    private JButton button9 = new JButton("");
    private String letter = "";
    private int count = 0;
    private boolean win = false;
    private JButton keys[];
    private JPanel keypad;
    private JTextField lcd;
    private double op1;
    private boolean first;
    private boolean foundKey;
    private boolean clearText;
    private int lastOp;

    //the actual constructed part of the program.
    public Game (){

    //construct components and initialize beginning values
    lcd = new JTextField(20);
    lcd.setEditable(false);
    keypad = new JPanel();
    keys = new JButton[9];
    first = true;
    op1 = 0.0;
    clearText = true;
    lastOp = 0;
    //Create Window
    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setLayout(new GridLayout(3,3));


    //Add Buttons To The Window
    window.add(button1);
    window.add(button2);
    window.add(button3);
    window.add(button4);
    window.add(button5);
    window.add(button6);
    window.add(button7);
    window.add(button8);
    window.add(button9);

    //create an instance of the menu
    MenuBar mnuBar = new MenuBar();
    window.setMenuBar(mnuBar);

    //construct and populate the File Menu
    Menu mnuFile = new Menu("File", true);
    mnuBar.add(mnuFile);
    MenuItem mnuFileExit = new MenuItem("Exit");
    mnuFile.add(mnuFileExit);

    //construct and populate the Edit menu
    Menu mnuEdit = new Menu("Edit", true);
    mnuBar.add(mnuEdit);
    MenuItem mnuEditClear = new MenuItem("Clear");
    mnuEdit.add(mnuEditClear);

    //construct and populate the About menu
    Menu mnuAbout = new Menu("About", true);
    mnuBar.add(mnuAbout);
    MenuItem mnuAboutGame = new MenuItem("About Game");
    mnuAbout.add(mnuAboutGame);

    //add the ActionListener to each menu
    mnuFileExit.addActionListener(this);
    mnuEditClear.addActionListener(this);
    mnuAboutGame.addActionListener(this);

    //assign and ActionCommand to each menu
    mnuFileExit.setActionCommand("Exit");
    mnuEditClear.setActionCommand("Clear");
    mnuAboutGame.setActionCommand("About");

    //*Add The Action Listener To The Buttons
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);


    addWindowListener(
    new WindowAdapter()
    {

    public void windowClosing(WindowEvent e)
    {
    System.exit(0);

    }
    }
    );

    //Need to see the window
    window.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    count++;

    //Calculate Who's Turn It Is
    if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
    letter = "X";
    } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
    letter = "O";
    }

    //Display X's or O's on the buttons
    if(e.getSource() == button1){
    button1.setText(letter);
    } else if(e.getSource() == button2){
    button2.setText(letter);
    } else if(e.getSource() == button3){
    button3.setText(letter);
    } else if(e.getSource() == button4){
    button4.setText(letter);
    } else if(e.getSource() == button5){
    button5.setText(letter);
    } else if(e.getSource() == button6){
    button6.setText(letter);
    } else if(e.getSource() == button7){
    button7.setText(letter);
    } else if(e.getSource() == button8){
    button8.setText(letter);
    } else if(e.getSource() == button9){
    button9.setText(letter);
    }


    //test for menu item clicks
    String arg = e.getActionCommand();
    if (arg == "Exit")
    System.exit(0);

    if (arg == "About")
    {
    String message = "Game ver. 2.0\nOpenSource Software\nCopyright 2009\nAll rights reserved";
    JOptionPane.showMessageDialog(null,message,
    "About Game", JOptionPane.INFORMATION_MESSAGE);


    }
    }



    public static void main(String args[])
    {
    //set frame properties
    Game f = new Game();

    }
    }

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

    Default

    Please use arrays to greatly simplify your code. Change this:
    Java Code:
      private JButton button1 = new JButton("");
      private JButton button2 = new JButton("");
      private JButton button3 = new JButton("");
      private JButton button4 = new JButton("");
      private JButton button5 = new JButton("");
      private JButton button6 = new JButton("");
      private JButton button7 = new JButton("");
      private JButton button8 = new JButton("");
      private JButton button9 = new JButton("");
    to this:
    Java Code:
      private JButton[][] buttons = new JButton[3][3];
    Then initializing the buttons, adding an actionlistener to them and them to the window can all be done in one simple for loop:
    Java Code:
        for (int i = 0; i < 3; i++)
        {
          for (int j = 0; j < 3; j++)
          {
            buttons[i][j] = new JButton("");
            buttons[i][j].addActionListener(this);
            window.add(buttons[i][j]);
          }
        }
    Then the actionPerformed code can be vastly simplified, and your clear method can be done with a simple nested for loop similar to the one I show you above. I'll leave it to you to figure out how to do that.

  13. #13
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    ok. but I do need help with that clear function. I have tried everything but it seems I've hit a brick wall. I can't figure things out very well.

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default odd or even?

    Java Code:
    if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
    letter = "X";
    } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
    letter = "O";
    }
    I think the above can be reduced to deducting if it's odd or even... maybe something like:

    Java Code:
    If (count % 2 == 0)
      {
        letter = "O"; //is even
      }
    else
      {
        letter = "X";//is odd
      }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  15. #15
    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 valiantofficer View Post
    ok. but I do need help with that clear function. I have tried everything but it seems I've hit a brick wall. I can't figure things out very well.
    Can you show us your new code using arrays, and then an attempt at a clear method, again using the button arrays?

  16. #16
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    2) Don't try to mix AWT and Swing components as you are doing as this is a recipe for disaster. I recommend that you use only Swing components here. Thus your Frame should be a JFrame. All Buttons should be JButtons. All TextFields should be JTextFields, all MenuBars should be JMenuBars, etc...
    I always use JFrame and JPanel, and the rest is AWT. xD (Don't ask me why, I don't know, honestly.)
    I die a little on the inside...
    Every time I get shot.

  17. #17
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Umm IDK If I can show it using arrays. I really just am trying to get a basic code working so I need my Clear Button to work. I wanna get this up on my site, but I still can't get the clear button to work.

    Java Code:
    /*
    Assignment 8: A Java Game of TicTacToe
    Filename: Game.java
    Purpose: To create a Game with a menu
    */
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    public class Game extends JFrame implements ActionListener
    
    {
    
    
    	//this keeps other programs from grabbing stuff from here
    	private JFrame window = new JFrame("Tic-Tac-Toe");
    	private JButton button1 = new JButton("");
    	private JButton button2 = new JButton("");
    	private JButton button3 = new JButton("");
    	private JButton button4 = new JButton("");
    	private JButton button5 = new JButton("");
    	private JButton button6 = new JButton("");
    	private JButton button7 = new JButton("");
    	private JButton button8 = new JButton("");
    	private JButton button9 = new JButton("");
    	private String letter = "";
    	private int count = 0;
    	private boolean win = false;
    private JButton keys[];
    private JPanel keypad;
    private JTextField lcd;
    private double op1;
    private boolean first;
    private boolean foundKey;
    private boolean clearText;
    private int lastOp;
    
    //the actual constructed part of the program.
    	public Game (){
    
    //construct components and initialize beginning values
    			lcd = new JTextField(20);
    			lcd.setEditable(false);
    			keypad = new JPanel();
    			keys = new JButton[9];
    			first = true;
    			op1 = 0.0;
    			clearText = true;
    		lastOp = 0;
    	//Create Window
    	window.setSize(300,300);
    	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	window.setLayout(new GridLayout(3,3));
    
    
    	//Add Buttons To The Window
    	window.add(button1);
    	window.add(button2);
    	window.add(button3);
    	window.add(button4);
    	window.add(button5);
    	window.add(button6);
    	window.add(button7);
    	window.add(button8);
    	window.add(button9);
    
    		//create an instance of the menu
    		MenuBar mnuBar = new MenuBar();
    		window.setMenuBar(mnuBar);
    
    		//construct and populate the File Menu
    		Menu mnuFile = new Menu("File", true);
    		mnuBar.add(mnuFile);
    		MenuItem mnuFileExit = new MenuItem("Exit");
    		mnuFile.add(mnuFileExit);
    
    		//construct and populate the Edit menu
    		Menu mnuEdit = new Menu("Edit", true);
    		mnuBar.add(mnuEdit);
    		MenuItem mnuEditClear = new MenuItem("Clear");
    		mnuEdit.add(mnuEditClear);
    
    		//construct and populate the About menu
    		Menu mnuAbout = new Menu("About", true);
    		mnuBar.add(mnuAbout);
    		MenuItem mnuAboutGame = new MenuItem("About Game");
    		mnuAbout.add(mnuAboutGame);
    
    //add the ActionListener to each menu
    mnuFileExit.addActionListener(this);
    mnuEditClear.addActionListener(this);
    mnuAboutGame.addActionListener(this);
    
    //assign and ActionCommand to each menu
    mnuFileExit.setActionCommand("Exit");
    mnuEditClear.setActionCommand("Clear");
    mnuAboutGame.setActionCommand("About");
    
    //*Add The Action Listener To The Buttons
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);
    
    add(lcd, BorderLayout.SOUTH);
    
    addWindowListener(
    new WindowAdapter()
    {
    	public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    
    }
    }
    );
    //Need to see the window
    	window.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
    count++;
    
    		//Display X's or O's on the buttons
    		if(e.getSource() == button1){
    			button1.setText(letter);
    		} else if(e.getSource() == button2){
    			button2.setText(letter);
    		} else if(e.getSource() == button3){
    			button3.setText(letter);
    		} else if(e.getSource() == button4){
    			button4.setText(letter);
    		} else if(e.getSource() == button5){
    			button5.setText(letter);
    		} else if(e.getSource() == button6){
    			button6.setText(letter);
    		} else if(e.getSource() == button7){
    			button7.setText(letter);
    		} else if(e.getSource() == button8){
    			button8.setText(letter);
    		} else if(e.getSource() == button9){
    			button9.setText(letter);
    		}
    //test for menu item clicks
    String arg = e.getActionCommand();
    if (arg == "Exit")
    System.exit(0);
    
    if (arg == "Clear")
    {
    clearText = true;
    first = true;
    op1 = 0.0;
    lcd.setText("");
    lcd.requestFocus();
    }
    if (arg == "About")
    {
    String message = "Game ver. 2.0\nOpenSource Software\nCopyright 2009\nAll rights reserved";
    JOptionPane.showMessageDialog(null,message,
    "About Game", JOptionPane.INFORMATION_MESSAGE);
    }
    }
      public static void main(String args[])
      {
      //set frame properties
      Game f = new Game();
    
    }
    }
    I need to get a TextField working, but I believe I can do that.

  18. #18
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Hehe. I JUST got my clear button to work. I just needed to change my IF statement regarding my clear button from
    Java Code:
    lcd.setText("");
    to
    Java Code:
    button1.setText("");
    button2.setText("");
    button3.setText("");
    button4.setText("");
    button5.setText("");
    button6.setText("");
    button7.setText("");
    button8.setText("");
    button9.setText("");
    Now I just need my TextField to output a message saying who won. Any ideas?
    I also need to find a way to stop the game from changing and X to an O and vica versa. If I click the same square twice, it'll change it over.
    Last edited by valiantofficer; 05-04-2009 at 06:10 PM.

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

    Default

    1) Your program will be a lot easier to code, improve, fix, update, etc... if you use arrays. Trust me. The time to change to arrays is now, not later after you have "the basic code working" because the longer you delay this the harder it will be.
    2) If you use arrays, your ActionListener will shrink to 1/9th the size.
    3) You will likely check the button's text in the action listener. If it's not empty (you can call something like
    Java Code:
    if (button.getText().isEmpty())
    {
      // set the button's text
    }
    4) Use one action listener for the buttons and a separate one for the menus. The actions of these two groups of controls are vastly different, and so they shouldn't be combined in the same actionlistener.
    5) This will fail eventually:
    Java Code:
    if (arg == "Clear")
    {
      //...
    You should compare Strings using the equals method or equalsIgnoreCase because if you use ==, you're not checking that the String text is the same but instead checking that one String object is the exact same object as another String object, and you really shouldn't care about this. You just want o see that the text are the same. So change this and similar constructs to:
    Java Code:
    if (arg.equals("Clear"))
    {
      //...
    Also, I like to use constant Strings here to minimize mistakes.
    Last edited by Fubarable; 05-05-2009 at 12:24 AM.

  20. #20
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Please keep in mind I am relatively new to java. What exactly is an array, and which lines specifically should be in one? Thanks for the help, btw. I just never had much instruction on this when I was in school.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  2. Java tictactoe
    By frans121 in forum New To Java
    Replies: 3
    Last Post: 02-19-2009, 03:54 AM
  3. TicTacToe
    By Thomas Covington in forum New To Java
    Replies: 1
    Last Post: 04-10-2008, 04:51 AM
  4. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 11:01 AM
  5. TicTacToe
    By Joe3161 in forum New To Java
    Replies: 4
    Last Post: 11-25-2007, 06:47 PM

Posting Permissions

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