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

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

到目前为止,这是我的代码:

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

我只是想知道如何让它工作。我以前在其他代码中使用过 Thread.sleep() 方法,但从未使用过 Graphics。我对异常也没有太多经验,我通常会尽力避免或纠正它们。

你永远不应该在事件调度线程(即在绘制方法中)调用 Thread.sleep 之类的方法。这将使整个 GUI 无响应。

您应该改用 SwingTimer 等计时器来执行动画等。请参阅以下相关问题:

  • how to use a swing timer to start/stop animation
  • Java Applet Thread Animation
  • How to make applet animation?
  • Drawing images continuously in Java Applet