I am doing a timer to countdown fro 90 seconds all the way down to zero, however when i run it, it will run for 1 second and terminate, plz help! Point out what is wrong!
package TestingFile;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class TestingATimer extends JFrame
{
private Timer timer;
public int count = 90;
public TestingATimer()
{
timer = new Timer(1000, new TimerListener());
timer.start();
}
private class TimerListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (count != 0)
{
count--;
System.out.println(count + " seconds elapsed");
}
}
}
public static void main (String [] args)
{
new TestingATimer ();
}
}
" however when i run it, it will run for 1 second and terminate"
Because you've set it to 1 second (1000 miliseconds) here:
If you want 90 seconds then do this:
The (Swing)
Timer
is likely using adaemon
based thread. This means that once themain
method exists, there is nothing keeping the JVM from terminating...From the
Thread
JavaDocsSo there is nothing stopping the JVM from terminating.
The question is, why are you using a
javax.swing.Timer
without a GUI? What are you trying to achieve?Updated
If you don't want to use a GUI, you will need to use a
java.util.Timer
, for example...Otherwise you'll need to supply some kind GUI...