|
-
Nov 12th, 2004, 03:47 PM
#1
Thread Starter
Frenzied Member
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.
-
Nov 14th, 2004, 01:38 PM
#2
Thread Starter
Frenzied Member
I was thinking there would have to be another method to do this, because in the above example, the only way to keep it going is like infinite recursion or something.
-
Nov 14th, 2004, 04:18 PM
#3
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2004, 06:05 AM
#4
Thread Starter
Frenzied Member
Thanks. I was hoping someone would have an easier method.
-
Nov 16th, 2004, 01:21 AM
#5
Originally posted by System_Error
Thanks. I was hoping someone would have an easier method.
Nope. Consider that your gift for today... happy birthday.
-
Nov 16th, 2004, 06:11 PM
#6
Thread Starter
Frenzied Member
Originally posted by mendhak
Nope. Consider that your gift for today... happy birthday.
ahhh...How sweet of you. Thank you!
-
Nov 17th, 2004, 03:20 AM
#7
Dazed Member
Had this hooked to a JButton once. Dunno if it helps.
Code:
public void actionPerformed(ActionEvent ae){
Thread timer = new Thread(this);
timer.start();
public void run(){
int seconds = 0;
int minutes = 0;
int hours = 0;
for(;;){
try{
Thread.sleep(1000);
seconds = ++seconds;
if(seconds == 60){
minutes = ++minutes;
seconds = 0;
}
if(minutes == 60){
hours = ++hours;
minutes = 0;
}
tt.timerscreen.setText(new String(hours+":"+ minutes+":"+ seconds));
}catch(InterruptedException e){;}
}
}
}
-
Nov 17th, 2004, 03:40 AM
#8
Dazed Member
Think i should have threw another if in there. Wouldn't want to go over 24 hrs.
-
Nov 17th, 2004, 05:24 PM
#9
Thread Starter
Frenzied Member
Thanks. That helps a bunch!
-
Nov 17th, 2004, 06:32 PM
#10
Dazed Member
Did it really? I wasen't sure if it would. My marijuana consumption has gone up dramatically within the past few weeks.
-
Nov 17th, 2004, 06:57 PM
#11
Thread Starter
Frenzied Member
Originally posted by Dilenger4
My marijuana consumption has gone up dramatically within the past few weeks.
Don't worry. It's only natural.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|