java - How to use Timer -
i have application made in netbeans , don't have idea how use timer
in java. in winform there's control box of timer
drag , use only. want use timer 1 seconds after about.seticon(about4);
(which gif) executed.
import javax.swing.imageicon; int a2 = 0, a3 = 1, a4 = 2; imageicon about2 = new imageicon(getclass().getresource("/2what-is-the-game.gif")); about2.getimage().flush(); imageicon about3 = new imageicon(getclass().getresource("/3how-to-play.gif")); about3.getimage().flush(); imageicon about4 = new imageicon(getclass().getresource("/4about-end.gif")); about4.getimage().flush(); if(a2 == 0) { a2=1; a3=1; about.seticon(about2); } else if (a3 == 1) { a3=0; a4=1; about.seticon(about3); } else if (a4 == 1) { a4=0; a2=0; about.seticon(about4); } }
how can achieve this?
in java, have several ways of timer implementation or rather uses, few of them are-
- to set specific amount of delay until task executed.
- to find time difference between 2 specific events.
timer class provides facility threads schedule tasks future execution in background thread. tasks may scheduled one-time execution, or repeated execution @ regular intervals.
public class javareminder { timer timer; public javareminder(int seconds) { timer = new timer(); //at line new thread created timer.schedule(new remindtask(), seconds*1000); //delay in milliseconds } class remindtask extends timertask { @override public void run() { system.out.println("remindertask completed java timer"); timer.cancel(); //not necessary because call system.exit //system.exit(0); //stops awt thread (and else) } } public static void main(string args[]) { system.out.println("java timer start"); javareminder reminderbeep = new javareminder(5); system.out.println("remindertask scheduled java timer."); } }
read more here:
Comments
Post a Comment