Results 1 to 4 of 4
- 11-13-2008, 02:55 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
beginner to create a countup timer
hi guys, i am new to java, and i want to create a timer that counts up that just look like this :
00 : 00 : 00 which corresponds to hr : min : sec
the only problem i m having right now is to "add" the leading zeros to the timer, when sec/min/hr is less than 10, because java only display 1 digit if the number is less than 10, if the number is 5, i want it to be "05"
i've done some code for this timer, please help me if i am doing anything wrong with the code, i want to just use basic java methods, using JFrame and If statements to make the timer, anyway here's my code, if possible please help me fill in the code which adds the leading zeros when the numbers are less than 10, i've been struggling with this for a long time, thanks alot guys:
public class ClockClass extends JFrame {
public Timer clock;
public int secs = 0;
public int mins = 0;
public int hrs = 0;
public JLabel display;
public ClockClass() {
setTitle("My Clock");
setLocation(20, 20);
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
FlowLayout layout = new FlowLayout();
setLayout( layout );
display = new JLabel();
add(display);
clock = new Timer(10, this);
clock.start();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == clock){
secs++;
}
if(secs == 60){
mins++;
secs = 0;
}
if(mins == 60){
hrs++;
mins = 0;
secs = 0;
}
if(hrs == 24){
hrs = 0;
mins = 0;
secs = 0;
}
display.setText(String.valueOf(hrs)+":"+String.val ueOf(mins)+":"+String.valueOf(secs));
}
public static void main(String[] args) {
new ClockClass();
}
}
-
DecimalFormat is what you need. You define it via:
Java Code:DecimalFormat dFormat = new DecimalFormat("00");
Java Code:import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import javax.swing.*; public class ClockClass extends JFrame implements ActionListener { public Timer clock; public int secs = 0; public int mins = 0; public int hrs = 0; public JLabel display; private DecimalFormat dFormat = new DecimalFormat("00"); public ClockClass() { setTitle("My Clock"); setLocation(20, 20); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); FlowLayout layout = new FlowLayout(); setLayout(layout); display = new JLabel(); add(display); clock = new Timer(10, this); clock.start(); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == clock) { secs++; } if (secs == 60) { mins++; secs = 0; } if (mins == 60) { hrs++; mins = 0; secs = 0; } if (hrs == 24) { hrs = 0; mins = 0; secs = 0; } display.setText( dFormat.format(hrs) + ":" + dFormat.format(mins) + ":" + dFormat.format(secs)); } public static void main(String[] args) { new ClockClass(); } }
- 11-13-2008, 06:35 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
thank you very much, it really helps me alot. all these time i've been trying to use the "if" statements to add leading zeros to it, lol, i could just add a simple decimalformat that i didnt know about, thanks alot
-
Similar Threads
-
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 09:14 AM -
total beginner needs little help
By asambasamba in forum New To JavaReplies: 1Last Post: 06-18-2008, 06:33 PM -
Beginner; Create a class to store info and constructor to initialize
By badness in forum New To JavaReplies: 16Last Post: 05-08-2008, 10:45 PM -
beginner to Java
By notwist in forum New To JavaReplies: 15Last Post: 04-18-2008, 10:41 AM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 03:46 PM
Bookmarks