简体   繁体   English

如何在NetBeans SWING中的循环中使用Thread.sleep?

[英]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() . 我正在NetBeans中制作一个化妆品SWING GUI程序,只是为了与朋友一起玩,它涉及一个加载屏幕,真的只需要几毫秒但我希望它花几秒钟完成,这是我想要的地方使用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. 加载是一个For-Loop,我知道你必须抛出InterruptedException,但是由于它是用NetBeans GUI构建器制作的,你无法添加它,我试过复制并粘贴到一个不同的项目但它只是想出来错误。 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. 所以我的问题是如何让Thread.sleep()工作或者是否有一些导致程序暂停的替代方法。 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. 切勿在Swing GUI应用程序中使用Thread.sleep() Alternatively use Swing Timers for the same behavior. 或者使用Swing Timers来实现相同的行为。

Please have a look about How to Use Swing Timers 请看看如何使用摆动计时器

Read more about Thread.Sleep alternative in Java 阅读更多关于Java中Thread.Sleep的替代方案


You can use Swing timers in two ways: 您可以通过两种方式使用Swing计时器:

  • 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. 例如,工具提示管理器使用Swing计时器来确定何时显示工具提示以及何时隐藏它。

  • To perform a task repeatedly. 反复执行任务。

    For example, you might perform animation or update a component that displays progress toward a goal. 例如,您可以执行动画或更新显示目标进度的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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