Results 1 to 5 of 5
Thread: 10 seconds countdown on console
- 04-08-2011, 06:31 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
10 seconds countdown on console
I wrote this program for a simple count down program. It compiles but nothing happens. Where have i gone wrong. I've never used Timers before:
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Timer; public class timer implements ActionListener{ Timer t; private int countdownPeriod = 0; private final int ONE_SECOND = 1000; private void write() { System.out.printf("%s, ", this.countdownPeriod); } public timer(int seconds) { this.countdownPeriod=seconds; t = new Timer(this.ONE_SECOND,this); t.setInitialDelay(0); t.start(); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(this.countdownPeriod==0) { t.stop(); }else{ this.countdownPeriod--; write(); } } public static void main(String args[]) { timer tom = new timer(10); } }
-
If you want a Timer in a non-GUI application, then don't use a Swing Timer (or javax.swing.Timer), but instead use a util Timer or java.util.Timer.
Having said that, some of your code would work with a simple Swing GUI:
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.Timer; public class MyTimer implements ActionListener { private JLabel timerLabel = new JLabel(" "); Timer t; private int countdownPeriod = 0; private final int ONE_SECOND = 1000; private void write() { //!! System.out.printf("%s, ", this.countdownPeriod); timerLabel.setText("" + countdownPeriod); } public MyTimer(int seconds) { this.countdownPeriod = seconds; timerLabel.setText("" + countdownPeriod); t = new Timer(this.ONE_SECOND, this); //t.setInitialDelay(0); t.start(); JOptionPane.showMessageDialog(null, timerLabel); } @Override public void actionPerformed(ActionEvent arg0) { if (this.countdownPeriod == 0) { t.stop(); } else { this.countdownPeriod--; write(); } } public static void main(String args[]) { MyTimer tom = new MyTimer(10); } }Last edited by Fubarable; 04-08-2011 at 06:37 PM.
- 04-08-2011, 06:35 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
So a swing timer won't work if there isn't as gui?
-
- 04-08-2011, 06:46 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Similar Threads
-
Finding epoch seconds and separate yera,month,day,hr,min and seconds
By sathish kumar in forum New To JavaReplies: 4Last Post: 09-09-2010, 11:15 AM -
how to countdown on server side
By BigBear in forum Java ServletReplies: 3Last Post: 04-26-2010, 10:33 PM -
a constructor to convert seconds to hours, min & seconds
By senca in forum New To JavaReplies: 3Last Post: 04-05-2010, 01:08 PM -
countdown timer, little help with method
By sidy in forum New To JavaReplies: 22Last Post: 07-19-2008, 12:42 PM -
CountDown timer
By Seema Sharma in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks