简体   繁体   中英

How to use Thread.sleep in a loop within NetBeans SWING?

I am making a cosmetic SWING GUI program, in NetBeans, just for some fun with friends and it involves a loading screen that really only takes a few milliseconds but I want it to take a few seconds to finish, which is where I am trying to use the Thread.sleep() . The loading is a For-Loop and I know you have to throw InterruptedException but being that it is made with the NetBeans GUI builder you can't add it, I have tried just copy and pasted into a different project but it just comes up with errors. So my question is how I would get the Thread.sleep() to work or if there is some alternative way of causing the program to pause. Thank you

Edit: Basically... How do I get a loop to pause every interval so that five loop intervals take five seconds.

Never use Thread.sleep() in Swing GUI application. Alternatively use Swing Timers for the same behavior.

Please have a look about How to Use Swing Timers

Read more about Thread.Sleep alternative in Java


You can use Swing timers in two ways:

  • To perform a task once, after a delay.

    For example, the tool tip manager uses Swing timers to determine when to show a tool tip and when to hide it.

  • To perform a task repeatedly.

    For example, you might perform animation or update a component that displays progress toward a goal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM