且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 中 javascript setTimeout 的等价物是什么?

更新时间:2022-10-15 08:31:38

我使用了 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.