Results 1 to 11 of 11

Thread: looping time{resolved}

Threaded View

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved looping time{resolved}

    I have a simple program that displays the time, date and stuff like that. The only problem is it just shows one constant time. I have tried several diffrent ways to try to loop it but I can't seem to get it to work. Here is the code I have so far:

    Code:
     
     import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class ClockPanel2 extends JPanel
    {
     public ClockPanel2()
     {
       super();
       String currentTime = getTime();
       JLabel time = new JLabel("Time: ");
       JLabel current = new JLabel(currentTime);
       add(time);
       add(current);
     }
     
     String getTime()
     {
     	String time;
    	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);
    	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 = "February";
    		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 = "August";
    		break;
    	case (9):
    		monthName = "September";
    		break;
    	case (10):
    		monthName = "October";
    		break;
    	case (11):
    		monthName = "November";
    		break;
    	case (12):
    		monthName = "December";
          }
    	time = monthName + " " + day + ", " + year + " " + hour + ":" + minute + ":" + second;
    	return time;
     }
    }
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ClockFrame2 extends JFrame
    {
     public ClockFrame2()
     {
       super("Clock");
       setSize(225,125);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container pane = getContentPane();
       FlowLayout flo = new FlowLayout();
       pane.setLayout(flo);
       ClockPanel2 time  = new ClockPanel2();
       pane.add(time);
       setContentPane(pane);
       setVisible(true);
      }
      
      
      public static void main(String[] args)
      {
       ClockFrame2 sal = new ClockFrame2();
      }
    }
    Anyone have an idea or a diffrent method on how to keep the time looping???
    Last edited by System_Error; Nov 15th, 2004 at 06:05 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width