Results 1 to 6 of 6
  1. #1
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default Value stored in variable not displaying.

    Hello, I'm hoping you can help with a simple problem of mine. I thought I had finally understood classes but it appears that I haven't :rolleyes:

    Here is my code:
    Java Code:
    public class days extends JFrame {
        String name;
    
    
        public days() {
    
                setSize(400,600);
    
                JPanel topPanel = new JPanel();
                topPanel.setLayout (new BoxLayout(topPanel, BoxLayout.Y_AXIS));
    
                JLabel label = new JLabel(name);
    
                topPanel.add(Box.createRigidArea(new Dimension(15,0)));
                topPanel.add(Box.createRigidArea(new Dimension(0,20)));
                topPanel.add(label);
    
                Container contentPane = getContentPane();
                contentPane.add(topPanel, BorderLayout.NORTH);
    
    
         }
    
        public static void main(String[] args) {
            
        
               days mon = new days();
               mon.name = "Monday";
               mon.setVisible(true);
    
          
      }
    }
    Basically, I want the word "Monday" to appear where the variable name is (within label). This should all work as I understand. I have made a public days class that can initialised as a object, in this case "mon". I then want the value mon.name to be "Monday", which should print out "Monday" where name is inside that object. Im getting pretty confused, any help?

    Thank you

  2. #2
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    I'm quite new to all of this but I'm going to try to help you.
    Your class and method have the same name, so it's a constructor.

    days mon = new days();
    Creates the JFrame and sets the name as a label.
    But you give mon.name the value "Monday" after the label is created so it doesn't do anything.

    Try to give the method another name and make the object, set the mon.name value and then call the method.

  3. #3
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    Thank you for the quick reply and I believe you are right, I just need a little help getting there.

    Try to give the method another name
    Ok, done that.
    Java Code:
     public void eachDay(String name) {
    
                setSize(400,600);
    
                JPanel topPanel = new JPanel();

    make the object, set the mon.name value
    Ok,
    Java Code:
     
    days mon = new days();
    mon.name = "Monday"; 
    mon.setVisible(true);

    and then call the method.
    This is the bit I dont have a clue how to do, sorry :( Im still very new to Java.

  4. #4
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    This way: (object name).(method name)();
    In this case mon.eachDay();
    Try this. :)

    Java Code:
    public class days extends JFrame {
        String name;
    
    
        public eachDay() {
    
                setSize(400,600);
    
                JPanel topPanel = new JPanel();
                topPanel.setLayout (new BoxLayout(topPanel, BoxLayout.Y_AXIS));
    
                JLabel label = new JLabel(name);
    
                topPanel.add(Box.createRigidArea(new Dimension(15,0)));
                topPanel.add(Box.createRigidArea(new Dimension(0,20)));
                topPanel.add(label);
    
                Container contentPane = getContentPane();
                contentPane.add(topPanel, BorderLayout.NORTH);
    
    
         }
    
        public static void main(String[] args) {
            
        
               days mon = new days();
               mon.name = "Monday";
               mon.eachDay();
               mon.setVisible(true);
    
          
      }
    }

  5. #5
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    :eek: How the hell did you know how to do that!???! It works perfectly, the only alteration to that method was to add void.

    Any idea where I can learn about classes? I've read through the class
    chapters in BigJava by Cay Hortsman and the Java for Dummies book. After reading them I believe I understand how they work fine, but when it comes to putting into use I can never get the things working.

    You have been awesome, thanks for all your help Bubbless.

  6. #6
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Glad I could help. :)
    If you need anything else, just ask.
    You could read the tutorials on the sun website.
    Here is the one about classes.
    (remove the space)
    http ://java.sun.com/docs/books/tutorial/java/javaOO/classes.html
    Last edited by bubbless; 03-10-2009 at 05:54 PM.

Similar Threads

  1. How to insert a Value into a stored array ?
    By A.M.S in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 03:21 PM
  2. stored procedure
    By sankarigopi in forum JDBC
    Replies: 1
    Last Post: 11-13-2008, 05:53 PM
  3. Stored Procedures
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 03:34 AM
  4. stored procedures in Hibernate
    By Alan in forum JDBC
    Replies: 2
    Last Post: 05-31-2007, 05:49 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
  •