Results 1 to 13 of 13

Thread: Java frames

  1. #1
    rev_ollie is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Java frames

    Hi,

    Have been reading along with Java and doing ok but I'm having difficults understanding some elements regarding GUI's = mainly Java frames and how to send results to them.

    I have been working on some code from a book - and making sure I understand it wanted to look through and see how it all works.However I'm not sure how some of the results have been send to the frame.

    This is what I have used so far...

    ClockPanel.java -
    Java Code:
    import javax.swing.*;
    import java.util.*;
    
    // C. Rogers Cadenhead
    
    public class ClockPanel extends JPanel {
        public ClockPanel() {
            super();
            String currentTime = getTime();
                    JLabel time = new JLabel ("Time: ");
                    JLabel current = new JLabel(currentTime);
                    add(time);
                    add(current);
                    
        }
        
        String getTime() {
            String time;
            //get current time and date
            Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
            int second = now.get(Calendar.SECOND);
            int month = now.get(Calendar.MONTH) +1;
            int day = now.get(Calendar.DAY_OF_MONTH);
            int year = now.get(Calendar.YEAR); 
          
    
            String timed = "" ;
            if (hour > 12 ) {
               // send result to jpanel!
            }
            else {
                System.out.println("It's the morning!");
            }
        
         
            String monthName = "";
            switch (month) {
                case (1):
                    monthName = "January";
                    break;
                case (2):
                    monthName= "Febuary";
                    break;
                case(3):
                    monthName= "March";
                    break;
                case(4):
                    monthName= "April";
                    break; 
               case(5):
                    monthName= "May";
                    break;
               case(6):
                    monthName= "June";
                    break;
               case(7):
                    monthName= "July";
                    break;
               case(8):
                    monthName= "Aughust";
                    break;
               case(9):
                    monthName= "September";
                    break;
               case(10):
                    monthName= "October";
                    break;
               case(11):
                    monthName= "November";
                    break;
               case(12):
                    monthName= "December";
                    break;                
            }
            time = monthName + " " + day + ", " + year + " "  + hour + ":" + minute + " " + timed;
            return time;
    }
    }
    ClockFrame
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    // C. Rogers Cadenhead
    
    public class ClockFrame extends JFrame {
        public ClockFrame() {
            super ("Clock");
            setSize(225, 125);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FlowLayout flo = new FlowLayout();
            setLayout(flo);
            ClockPanel time = new ClockPanel();
            add(time);
            setVisible(true);
            
        }
        
        public static void main (String[] arguments) {
            ClockFrame clock = new ClockFrame();
        }
    }
    If you notice on the ClockPanel.java script there is an If statement which I have added in.
    Now the result of that would either be - System.out.println("It's the morning!"); or System.out.println("It's the afternoon!"); (the afternoon part I replaced with a comment while I was working on it.

    However when I run that obviously the panel opens to display the time and date - but the result of the if statement opens in the console (within net beans).

    I need to try and work out how the original result is sent to the java panel. I understand that the ClockFrame script calls the time string (think its a string...) from the ClockPanel script.

    I believe that I need to add something to the ClockPanel script under the first string (again if my terminology is correct).
    Am I looking in the correct area there?

    So would add something within:
    Java Code:
    public class ClockPanel extends JPanel {
        public ClockPanel() {
            super();
            String currentTime = getTime();
                    JLabel time = new JLabel ("Time: ");
                    JLabel current = new JLabel(currentTime);
                    add(time);
                    add(current);
                    
        }
    I presume then I would call entry in here in the time string at the bottom of the page...

    I think I might have got into a pickle with my varied understanding and hope someone can point me in the right direction. I'm ok finding it out I could just do with some direction of what I maybe need to read up on more for example.

    Thanks everyone

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Java frames

    Java frames and how to send results to them.
    I assume you mean how do display some text on the GUI presented in a JFrame.
    There are several components you can put in a GUI where you can display text: text fields, text areas and labels.

    You should read up on how to build GUIs and what each of the components are used for.
    Take a look at the Java Tutorial The Really Big Index
    Find the Creating a GUI section.

  3. #3
    rev_ollie is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Java frames

    Ah wow nice link thanks!

    Gradually understanding that now.

    Another quick question that's hampering me a little:

    In the top section of the code there is:
    Java Code:
    public class ClockPanel extends JPanel {
        public ClockPanel() {
            super();
            String currentTime = getTime();
             JLabel time = new JLabel("Time: ");   
             add(time);
             JLabel current = new JLabel (currentTime);
             add(current);
        }
    Line 4 there is the line String currentTime = getTime();.
    How come you have to use that instead of just calling getTime() in the JLabel?

    If you do it comes up with "cannot fine symbol". So is this a way of almost locating it...not sure I get that line and why its there.

    Sorry if its a somewhat simple question but I am determined not to give up on this code!!

    Cheers,

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Java frames

    How come you have to use that instead of just calling getTime() in the JLabel?
    All methods belong to some class. If a class does NOT have the method then the compiler will say it can not find it.
    Does the ClockPanel have a getTime() method? If it does,the compiler is happy.
    Does the JLabel class have a getTime() method? If it does not, the compiler complains.

    You could call the ClockPanel's getTime() method by passing a reference to a ClockPanel object and using that:
    refToClockPanelClass.getTime();

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

    Default Re: Java frames

    Please show us the code where you tried to use it and where you got the error. Also please post the actual error message itself and indicate which line causes it to occur.

  6. #6
    rev_ollie is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Java frames

    Ok here goes:

    Java Code:
    import javax.swing.*;
    import java.util.*;
    
    
    public class ClockPanel extends JPanel {
        public ClockPanel() {
            super();
            String currentTime = getTime();
             JLabel time = new JLabel("Time: ");   
             add(time);
             JLabel current = new JLabel (currentTime);
             add(current);
        }
        
        String getTime() {
            String time;
            //get current time and date
            Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
            int second = now.get(Calendar.SECOND);
            int month = now.get(Calendar.MONTH) +1;
            int day = now.get(Calendar.DAY_OF_MONTH);
            int year = now.get(Calendar.YEAR);  
                   
            
            String monthName = "";
            switch (month) {
                case (1):
                    monthName = "January";
                    break;
                case (2):
                    monthName= "Febuary";
                    break;
                case(3):
                    monthName= "March";
                    break;
                case(4):
                    monthName= "April";
                    break; 
               case(5):
                    monthName= "May";
                    break;
               case(6):
                    monthName= "June";
                    break;
               case(7):
                    monthName= "July";
                    break;
               case(8):
                    monthName= "Aughust";
                    break;
               case(9):
                    monthName= "September";
                    break;
               case(10):
                    monthName= "October";
                    break;
               case(11):
                    monthName= "November";
                    break;
               case(12):
                    monthName= "December";
                    break;    
                   
                    String timeOfDay()
                   if (hour < 12) {
                       timeOfDay = "Morning";          
                   } else {
                       timeOfDay = "Afternoon";
                   }
                         
            }
            
            time = day + " " + monthName + " " + year + " " + hour + ":" + minute + ":" + second + timeOfDay;
            return time;
        }             
    }
    SO this is what I have so far.
    Line 66 is where I am trying to start the if statement - again I'm not sure if that's the right place to start it off. At line 66 I get the error "; expected". I would presume I have used incorrect syntax somewhere there...

    The other error comes at line 75 where I the error is presented over "timeOfDay" stating:
    "cannot find symbol
    symbol: variable timeOfDay
    location: class ClockPanel"

    Again apologies if its simple of vague but I will hopefully get there soon enough...

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Java frames

    Your definition(???) of timeOfDay looks like a method call with the ending ()s
    If that is where you intend to define a String, remove the ()s

  8. #8
    rev_ollie is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Java frames

    Ah ok well that has tidied that up then. So that leave the cannot find symbol error on line 75.

    If that error is coming up then would I do as you suggested earlier and reference back to the string at line 66?

    I'm not sure if this is the best way to go - its merely what I have picked up - the intention being to find the hour (which is found at line 19) then find the result either being morning or afternoon and reference back to that in the time statement at the bottom which would pull it through to the JLabel current...

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Java frames

    cannot find symbol error on line 75.
    Please post the full text of the current error message.

    Is the definition of the variable inside of a pair of {}s that hides it?
    You need to define the variable within the same pair of {}s where it is used.
    Last edited by Norm; 09-06-2011 at 12:05 AM.

  10. #10
    rev_ollie is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Java frames

    This is the full error I get at line 75 (red line under timeOfDay;)

    Java Code:
    cannot find symbol
      symbol:   variable timeOfDay
      location: class ClockPanel
    ----
    (Alt-Enter shows hints)

    Ah might want this to when its compiled:

    Java Code:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
      symbol:   variable timeOfDay
      location: class ClockPanel
    	at ClockPanel.getTime(ClockPanel.java:75)
    	at ClockPanel.<init>(ClockPanel.java:8)
    	at ClockFrame.<init>(ClockFrame.java:13)
    	at ClockFrame.main(ClockFrame.java:20)
    Java Result: 1

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Java frames

    Where is that symbol defined? Is it in scope for where you are trying to reference it?
    See end of post #9

  12. #12
    rev_ollie is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Java frames

    Ah Norm you are a genius!

    Wasn't defined in the correct place. Was a sneaky } just above it that I just wasn't seeing.
    Has picked it up nicely now and it's all working how I expected it to - and as such confusing me a heck of allot less than It was before!

    Thanks again for sticking with me - really appreciated

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

Similar Threads

  1. frames
    By giorgi in forum AWT / Swing
    Replies: 2
    Last Post: 05-04-2011, 09:45 PM
  2. Java Frames
    By Java Unknown in forum New To Java
    Replies: 2
    Last Post: 02-22-2011, 06:45 AM
  3. Problem with java frames
    By JavaCy in forum Java Applets
    Replies: 33
    Last Post: 09-13-2010, 04:44 PM
  4. What are the hot java frames works on demand
    By mallaravi in forum Web Frameworks
    Replies: 1
    Last Post: 10-28-2008, 02:34 PM
  5. will java webBrowser work with frames
    By elipford in forum New To Java
    Replies: 2
    Last Post: 06-04-2007, 04:26 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
  •