Java 中 javascript setTimeout 的等价物是什么? [英] What is the equivalent of javascript setTimeout in Java?

查看:42
本文介绍了Java 中 javascript setTimeout 的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个在单击按钮 60 秒后运行的函数.请帮忙,我使用了 Timer 类,但我认为这不是最好的方法.

解决方案

我使用了 Timer 类,但我认为这不是最好的方法."

其他答案假设您的用户界面(按钮)没有使用 Swing.

如果您正在使用 Swing,那么请不要使用 Thread.sleep(),因为它会冻结您的 Swing 应用程序.

相反,您应该使用 javax.swing.Timer.

请参阅 Java 教程如何使用 Swing 计时器课程:Swing 中的并发 了解更多信息和示例.>

I need to implement a function to run after 60 seconds of clicking a button. Please help, I used the Timer class, but I think that that is not the best way.

解决方案

"I used the Timer class, but I think that that is not the best way."

The other answers assume you are not using Swing for your user interface (button).

If you are using Swing then do not use Thread.sleep() as it will freeze your Swing application.

Instead you should use a javax.swing.Timer.

See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.

这篇关于Java 中 javascript setTimeout 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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