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

    Default

    I thought i answered the instance variable right above...some of the other instance variable used in my code are referenced by "private ..." but i dont know what you mean by p is local to the constructor. What can i do to make the panel an instance variable?? add it to that "private" list somehow? And i dont know how i would set up the panel in the constructor.

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

    Default

    Your frame is an instance variable. Your panel is local to the constructor. Change the panel to like the frame.

    My crystal ball predicts a MFU.

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

    Default

    Ill walk you through the questions, but you would get more help googling and reading where you can.

    First, local refers to scope of something. When you declare something, it lasts until the correct closing brace
    Java Code:
    public class X{
      private int x; //this is an instance variable, it has a scope of anywhere in the class 
                        //meaning it can be used anywhere in the class
      
      public X(){
        int y; //this is local to the constructor, it will only exist in the constructor
        y = 0;
        x = 0; //I can use x here because it's available anywhere in the class
      }
      
      public int[] someMethod(){
        int[] xyz = new [5]; //this is local during this method, not outside though
        for(int i = 0; i < xyz.length; i++){ //here i is local to this loop, it doesn't exist after
                                                      //the loop
          int z = Math.random() * 3;  //this is also local to the loop and doesn't exist outside the loop
         xyz[i] = z;
        }
        return xyz;
      }
    }
    That's a decent explanation of scope I believe.

    Next, instance variables are classes that need to be instantiated when the class is called(if there is a default constructor objects are made null, numbers are 0 or 0.0, I believe boolean is made false)

    They can be used anywhere in the class(other methods, constructors, etc) Instance variables are generally set up in the constructor, but can be set up in methods as well. For instance, I like to do gui programming with the use of a method that sets anything up(I am new to this as well so it may not be the best, but as far as I know it doesn't matter which way it's done)

    the constructor sets up everything for you(it constructs an instance of the class) You are correct, the list of things with "private ..." are instance variables. They don't NEED to be private but in general it's best to have them be private. Lets say I have a JFrame as an instance variable I would do this

    Java Code:
    public class X{
      private JFrame frame;
      public X(){
        frame = new JFrame();
        frame.setSize(100, 100);
        frame.setVisible(true);
      }
    }

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

    Default

    Now if you want that JPanel p to be an instance variable, put "JPanel p;" right under where it says "JFrame window = etc...". Then delete the first word "JPanel" in the constructor.

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

    Default

    This what you mean.thanks sunde and ra4king its coming much clearer now

    Java Code:
    public class TicTacToe1 extends JApplet implements ActionListener{
    
    
    	private char whoseTurn = 'X';// Shows which player has a turn
    	private Cell[][] cells = new Cell[3][3];// Create the cells here
    	private JLabel jlblStatus = new JLabel("X's turn to play");// Create and initialize a status label
    	private JFrame window = new JFrame("Tic-Tac-Toe");
        private JPanel p;

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

    Default

    Finally *sigh* ;)

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

    Default

    Is this what you were looking for too?? It still doesnt clear anything when i click the button haha.
    Java Code:
    public TicTacToe1() { // Initialize User Interface)
    
        p = new JPanel(new GridLayout(3, 3, 0, 0));
        for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            p.add(cells[i][j] = new Cell());

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

    Default

    No you should put those lines of code in their own method, aka clearTheBoard()?

    Java Code:
    public void clearTheBoard() {
        //put that code here
    }

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

    Default

    You want to create a method that does that for you(places the squares on the panel), then you call that method to set it up in the constructor. You also need to add an action listener to the clear button like this

    Java Code:
    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        clearBoard();
      }
    });
    or you actually only have one actionPerformed method so..

    Java Code:
    public void actionPerformed(ActionEvent e){
      ...
      if(actionCommand.equals("Clear")){
        clearBoard();
      }
    }

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

    Default

    well dont i need that code to start off the board? the app isn't even coming up if i rem out those lines.

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

    Default

    You have to put that method in the constructor and the listener for the clear button.

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

    Default

    ok so my clear button won't work or will it because this is what i have right now

    Java Code:
    JMenuItem clearAction = new JMenuItem("Clear");//Clear Item
    	fileMenu.add(clearAction);
         clearAction.setActionCommand("Clear");
          clearAction.addActionListener(this);

  13. #53
    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 don't know, you need to test it and THINK about what should happen.

    Do you have a method that sets up a blank board? If you do, what is it? Is a blank board the same as a cleared board? So when you click the button how would you make the board blank?

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

    Default

    Well these are all the important parts that we are talking about i did make a new class public void clear board class and i call it when if "clear" button is clicked. But i dont see my problem.


    Java Code:
    private char whoseTurn = 'X';// Shows which player has a turn
    	private Cell[][] cells = new Cell[3][3];// Create the cells here
    	private JLabel jlblStatus = new JLabel("X's turn to play");// Create and initialize a status label
    	private JFrame window = new JFrame("Tic-Tac-Toe");
        JPanel p;
    Java Code:
    public TicTacToe1() { // Initialize User Interface)
    
        //p = new JPanel(new GridLayout(3, 3, 0, 0));
        //for (int i = 0; i < 3; i++)
        //for (int j = 0; j < 3; j++)
            //p.add(cells[i][j] = new Cell());
    
    p.setBorder(new LineBorder(Color.black, 1));  // Set line borders on the cells panel and the status label
        jlblStatus.setBorder(new LineBorder(Color.black, 1));
        add(p, BorderLayout.CENTER);//position the Border Layout
        add(jlblStatus, BorderLayout.SOUTH);
    Java Code:
    JMenuItem clearAction = new JMenuItem("Clear");//Clear Item
    	fileMenu.add(clearAction);
         clearAction.setActionCommand("Clear");
          clearAction.addActionListener(this);
    Java Code:
    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());
    
    	}
      }
    }

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

    Default

    Alright, does it compile? Does it run?

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

    Default

    No because i remmed out those lines at the top which is meant to be there to start the game if thats not there it won't recognize the panel at all. If i un-rem it there are no errors but nothing happens when i click clear.

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

    Default

    Post the code that doesn't work. and copy/paste the exact errors you receive(if you do get errors)

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

    Default

    Since you told me to put this code in another class or method i just remmed it out which shouldnt make a difference. But if i try to run the program with this code i get an error saying:

    Java Code:
    public TicTacToe1() { // Initialize User Interface)
    
        //p = new JPanel(new GridLayout(3, 3, 0, 0));
        //for (int i = 0; i < 3; i++)
        //for (int j = 0; j < 3; j++)
            //p.add(cells[i][j] = new Cell());
    Java Code:
    Exception in main thread "main" java.lang.NullPointerException
    The panel doesnt even come up its the window\system\cmd prompt that shows this error

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

    Default

    When the system starts how is it going to know what the layout of the panel is going to be if i put it in another method??

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

    Default

    Ok, un-rem those lines, and in clearTheBoard(), change the line that says "p.add(cell[i][j] = new Cell());" to just "cell[i][j] = new Cell();"

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