简体   繁体   English

如何成功使用Graphics中的Thread.sleep()方法?

[英]How do I successfully use the Thread.sleep() method in Graphics?

This is my code so far: 到目前为止,这是我的代码:

// Imported Classes
public class Timer extends Applet
{
    public void paint (Graphics page) throws InterruptedException
    {
        Thread.sleep(1000);
    }
}

I just want to know how I can get this to work. 我只是想知道如何使它工作。 I've used the Thread.sleep() method in other code before, but never with Graphics. 我以前在其他代码中使用过Thread.sleep()方法,但从未在Graphics中使用过。 I don't have much experience with Exceptions either, I usually try my best to avoid or correct them. 我也没有太多关于异常的经验,我通常会尽力避免或纠正它们。

You should never call methods such as Thread.sleep on the event dispatch thread (ie in paint methods). 您永远不要在事件分配线程上调用诸如Thread.sleep之类的方法(即在paint方法中)。 This will render the whole GUI unresponsive. 这将使整个GUI无法响应。

You should instead use timers such as SwingTimer to perform animations etc. See the following related questions: 您应该改用诸如SwingTimer计时器来执行动画等。请参阅以下相关问题:

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

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