在java中处理计时器 [英] Handling timers in java

查看:33
本文介绍了在java中处理计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它运行一个计时器来检查空闲时间,一旦 10 秒内没有活动,应用程序就会关闭.我有近 100 个屏幕,我想跟踪所有屏幕上的不活动秒数.我很难将所有按钮、文本框、标签中的处理事件一一写出来.我要做的是在用户在应用程序上的每个操作上添加 10 秒.即使是 mousemove 添加 10 秒,所以应用程序不会再关闭 10 秒.有什么办法可以有效地处理这个问题吗?

I have an application which runs a timer to check for idle time and once there is no activity for 10 seconds the application will close. I have nearly 100 screens and i want to track the inactivity seconds on all the screens. Its hard for me to write the handling events in all buttons, textboxes, labelboses one by one. What i have to do is add 10 seconds on every action of the user on the application. Even if it is mousemove add 10 seconds so tat the application wont close for another 10 seconds. Is there any way to handle this effectively ?

推荐答案

我建议使用以下处理程序:

I would suggest the following handler:

final Timer tm = new Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("10 SECONDS AND NOTHING HAPPENED");
    }
});
tm.start();
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {    
    @Override
    public void eventDispatched(AWTEvent event) {
        tm.restart();
    }
}, -1);

这篇关于在java中处理计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆