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

    Default

    So if i were to create a seperate method public clear or whatever then add the code then how would i call that? I think im mixing up a method and a class im really stumped sorry guys.

    Java Code:
    if (arg.equals("Clear")){
    
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());

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

    Default

    FFS
    Java Code:
    actionPerformed() {
        if clear button
            createNewBoard();
        }
    }
    
    createNewBoard() {
        code goes here
    }
    The advantage is that you move the code that you currently have in the constructor into the method. You then call that method from the constructor and the actionPerformed method whenever you need a new board. Thus you do not duplicate code.

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

    Default

    This is supposed to be an action listener attached to your clear button. Since you attached all listeners with a reference to the class(use of this). You probe where the event came from and do something based on where it came from. Clearing the board should be handled in this action listener, try adding it, then compile, run, and test by setting a few x's and o's then clearing it.

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

    Default

    I see now..just one more problem i need to get over what would i use for a line to set the cells to clear..? set. or? system.print? I'm still not quite sure but i understand what your telling me though

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

    Default

    This is what i have so far haha im stuck as to what to do

    Java Code:
    if (arg.equals("Clear")){
    
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());
    If i have tanslated what you said then you want something like this

    Java Code:
     public void actionPerformed(ActionEvent e){//When action is clicked the app will exit
         String arg = e.getActionCommand();{
         if (arg.equals("Exit")){
         System.exit(0);
    }
    
    if (arg.equals("Clear")){
      clearTheBoard();
    }
    }
    
    clearTheBoard() {
    
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());
    }

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

    Default

    This is what i got and i have 1 error anybody have any ideas?

    Java Code:
    public void actionPerformed(ActionEvent e){//When action is clicked the app will exit
         String arg = e.getActionCommand();{
         if (arg.equals("Exit")){
         System.exit(0);
    }
    
    if (arg.equals("Clear")){
      clearTheBoard();
    
    }
    
    
    clearTheBoard(){
    
    	for (int i = 0; i < 3; i++){
    	    for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());
    }
    }
    
    if (arg == "About"){
        String message = "Ryan's TicTacToe \nStructured Programming\n2011";
        JOptionPane.showMessageDialog(null,message,
        "About TicTacToe", JOptionPane.INFORMATION_MESSAGE);//About message when "about" is clicked
        }
      }
    }
    }
    Java Code:
    F:\TicTacToe\TicTacToe1.java:68: ';' expected
    clearTheBoard(){
                   ^
    1 error
    
    Tool completed with exit code 1

  7. #27
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You have added an extra curly brace next to the for loop inside clearTheBoard() :P

    EDIT: Whoa! You have put the method inside the actionPerformed() method?? You have to put it outside it!
    Last edited by ra4king; 04-12-2011 at 02:13 AM.

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

    Default

    If i delete that brace i get 3 errors then:

    Java Code:
     public void actionPerformed(ActionEvent e){//When action is clicked the app will exit
         String arg = e.getActionCommand();{
         if (arg.equals("Exit")){
         System.exit(0);
    }
    
    if (arg.equals("Clear")){
      clearTheBoard();
    }
    
    
    clearTheBoard();
    
    	for (int i = 0; i < 3; i++){
    	    for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());
    }
    
    
    
    if (arg == "About"){
        String message = "Ryan's TicTacToe \nStructured Programming\n2011";
        JOptionPane.showMessageDialog(null,message,
        "About TicTacToe", JOptionPane.INFORMATION_MESSAGE);//About message when "about" is clicked
        }
      }
    }
    }
    Java Code:
    F:\TicTacToe\TicTacToe1.java:64: cannot find symbol
    symbol  : method clearTheBoard()
    location: class TicTacToe1
      clearTheBoard();
      ^
    F:\TicTacToe\TicTacToe1.java:68: cannot find symbol
    symbol  : method clearTheBoard()
    location: class TicTacToe1
    clearTheBoard();
    ^
    F:\TicTacToe\TicTacToe1.java:72: cannot find symbol
    symbol  : variable p
    location: class TicTacToe1
            p.add(cells[i][j] = new Cell());
            ^
    3 errors
    
    Tool completed with exit code 1

  9. #29
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Look at my EDIT in my previous post.

    You have put the method inside the actionPerformed method. You have to put it oustide it:
    Java Code:
    public void actionPerformed(ActionEvent ae) {
        //etc...
        if(arg.equals("Clear")) {
            clearTheBoard();
        }
        //etc...
    }
    
    public void clearTheBoard() {
        //etc...
    }

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

    Default

    I dont know where everyone else went...they just disappeared
    Last edited by Ryan10; 04-12-2011 at 02:36 AM.

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

    Default

    Still have one error

    Java Code:
    public void actionPerformed(ActionEvent e){//When action is clicked the app will exit
         String arg = e.getActionCommand();{
         if (arg.equals("Exit")){
         System.exit(0);
    }
    
    if (arg.equals("Clear")){
      clearTheBoard();
    }
    
    if (arg == "About"){
        String message = "Ryan's TicTacToe \nStructured Programming\n2011";
        JOptionPane.showMessageDialog(null,message,
        "About TicTacToe", JOptionPane.INFORMATION_MESSAGE);//About message when "about" is clicked
        }
      }
    }
    
    public void clearTheBoard() {
    
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            p.add(cells[i][j] = new Cell());
    
    	}
      }
    }
    Java Code:
    F:\TicTacToe\TicTacToe1.java:80: cannot find symbol
    symbol  : variable p
    location: class TicTacToe1
            p.add(cells[i][j] = new Cell());
            ^
    1 error
    
    Tool completed with exit code 1

  12. #32
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Ah that's because "p" is not a global variable. Under "JFrame frame = etc...", add "JPanel p;" and in your constructor, delete the first "JPanel" word

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

    Default

    Can you please take snippets out of the code to what spots you are talking about :) we are almost there!

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

    Default

    the panel these cells are on is local to the constructor. having it be an instance variable will allow you to access it in other methods(like createBoard())

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

    Default

    Still not sure what to do here..

    Java Code:
    public static void main(String[] args) {
    // Create a frame
         JFrame frame = new JFrame("TicTacToe");
         JPanel p = new JPanel(); <<<<<<<<<<<<<<<<<<<IS this what you meant?
           TicTacToe1 applet = new TicTacToe1();
         frame.add(applet, BorderLayout.CENTER);

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

    Default

    No, you want the panel to be a part of the class, creating it in main does nothing.

    You can't just guess, take a minute to think about what the problem is. What does it mean if I say p is local to the constructor? What is an instance variable? Name a few in your tictactoe class. What can you do to make the panel an instance variable? What kind of set up for the panel would be needed in the constructor?

    These are some of the questions you need to answer to understand the problem.

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

    Default

    Instance variable..am i close? A instance variable are basically variables that are in non-static fields

    Java Code:
    JPanel p = new JPanel();
    Last edited by Ryan10; 04-12-2011 at 03:06 AM.

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

    Default

    ???
    Am i even close guys

    Java Code:
    private JPanel p = new JPanel();

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

    Default

    wow i have no idea what to do now

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

    Default

    I asked you quite a few questions, please try and answer them for me, and Ill know how well you understand the problem. The best way to learn how to fix problems is to experience them, and think through the reason they occur.

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Clear/Reset Button Problem
    By Ryan10 in forum New To Java
    Replies: 10
    Last Post: 04-12-2011, 04:04 PM
  2. Solving a maze in java
    By kuro in forum New To Java
    Replies: 4
    Last Post: 04-12-2011, 12:41 AM
  3. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 06:21 PM
  4. Solving a Maze
    By bdario1 in forum New To Java
    Replies: 4
    Last Post: 04-14-2010, 01:02 AM
  5. I need help solving this problem.
    By Felicia in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 09:40 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
  •