System_Error
Nov 12th, 2004, 02:47 PM
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:
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;
}
}
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???
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;
}
}
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???