What is the equivalent of javascript setTimeout in Java?

前端 未结 9 876
梦毁少年i
梦毁少年i 2020-12-13 06:23

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.

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 06:38

    "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.

提交回复
热议问题