java提供了import javax.swing.Timer;中有个Timer类你只要定义:ActionListener listener=new TimePrinter();
Timer t=new Timer(100,listener);//100指的是100毫秒
然后监听事件:
private class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
run();//每100毫秒发生一次
}
}
Timer t=new Timer(100,listener);//100指的是100毫秒
然后监听事件:
private class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
run();//每100毫秒发生一次
}
}
long delay,
long period)
import java.util.*;
class matimerTask extends TimerTask{
public void run(){
System.out.println("timer task executed");
}
}class ttest{
public static void main(String[] args){
myTimerTask mytask=new myTimerTask();
Timer mytimer=new Timer();
mytimer.schedule(mytask,1000,500);//程序开始一秒后每个半秒执行mytask一次
try{
Thread.sleep(5000);
}catch(InterruptedException exc){}
mytimer.cancel();
}
}