Java Swing: Change Text after delay

后端 未结 3 1494
一向
一向 2020-12-06 23:08

Basically, I have this game where once guesses the correct answer it starts a new game with a new word. I want to display Correct! but after three seconds, chan

3条回答
  •  感动是毒
    2020-12-06 23:35

    Swing is an event driven environment. While you block the Event Dispatching Thread, no new events can be processed.

    You should never block the EDT with any time consuming process (such as I/O, loops or Thread#sleep for example).

    You might like to have a read through The Event Dispatch Thread for more information.

    Instead, you should use a javax.swing.Timer. It will trigger a ActionListener after a given delay.

    The benefit of which is that the actionPerformed method is executed with the context of the Event Dispatching Thread.

    Check out this or this or this or this for an examples

提交回复
热议问题