Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Down Timer

Status
Not open for further replies.

jodders

Programmer
Nov 12, 2003
13
0
0
GB
Im trying to display a count down timer and i dont know how to display the time left after every second. For example,if a user types in 5, i want to display the time going 5 ,4 ,3 ,2,1

I think im close,but i need some help.The code below almost works.


Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;

public class Temp extends JFrame implements ActionListener{	
/**	 * @param countdown number of milliseconds	 */	

public Temp(int countdown) {		
super();		
setVisible(true);		
Timer timer = new Timer(countdown, this);		
timer.start();	
}	


public void actionPerformed(ActionEvent e) 
{		
System.out.println("done");	

}	

public static void main(String args[]) {		
Temp t = new Temp(5000);	
}

}
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;

public class Temp extends JFrame implements ActionListener{
/** * @param countdown number of milliseconds */
int second;
Timer timer;
public Temp(int countdown,int input) {
super();
setVisible(true);
if (input==0) second=5;
else
second = input;
timer = new Timer(countdown, this);
timer.start();
}


public void actionPerformed(ActionEvent e)
{
System.out.println(second);
second--;
if (second==0)
{
System.out.println("Stop!!!");
timer.stop();
//System.exit(0);
}
}

public static void main(String args[])
{
Temp t;

if (args.length==0)
t = new Temp(1000,5);
else
t = new Temp(1000,Integer.parseInt(args[0]));
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top