java - Count Down Timer? -
java - Count Down Timer? -
i create simple application contain quiz questions , user select reply need help in adding count downwards timer in app 20 sec when time transfer straight next question , when user reply in time transfer next question
thank
import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.timer; import javax.swing.windowconstants; public class countdown extends jframe { // countdown 42 seconds public static int countervalue = 42; public static timer timer; public static jlabel label; public countdown() { initgui(); } private void initgui(){ borderlayout thislayout = new borderlayout(); this.setdefaultcloseoperation(windowconstants.dispose_on_close); this.getcontentpane().setlayout(thislayout); label = new jlabel(); label.settext(string.valueof(countervalue)); this.getcontentpane().add(label, borderlayout.center); this.settitle("countdown example"); this.pack(); this.setvisible(true); } public static void main(string[] args) { countdown countdown = new countdown(); countdown.timer = new timer(1000, new actionlistener() { public void actionperformed(actionevent e) { // =1 sec countdown.countervalue--; countdown.label.settext(string.valueof(countervalue)); if(countdown.countervalue == 0){ system.out.println("counterdown ausgelaufen!"); // timer stop countdown.timer.stop(); } } }); // timer start timer.start(); } }
taken http://blog.mynotiz.de/programmieren/java-countdown-und-timer-am-beispiel-von-swing-1707/ ( high german )
java android eclipse
Comments
Post a Comment