While loop with delay makes JFrame unresponsive

后端 未结 1 1856
予麋鹿
予麋鹿 2021-01-17 04:07

So, my JFrame becomes unresponsive when I run this code. I managed to trace it back to the while loop under gameLoop(). Regardless of using delay(1000/FRAMERATE) which calls

1条回答
  •  隐瞒了意图╮
    2021-01-17 04:22

    You are performing Thread.sleep() in your main thread which in this case is the EDT (Event Dispatcher Thread). This thread is responsible for painting your graphics and listening to events. By doing Thread.sleep() you are essentially pausing it, thus not allowing it to do it's job.

    It seems that you want to refresh your GUI at intervals (at least that is what I am guessing). If this is the case, you will need to move your logic to a new separate thread and then, call what ever update methods you will need from the thread you will have spawned off. To make changes visible, you will need to call the event dispatcher thread again by using the SwingUtilities.invokeLater() method.

    0 讨论(0)
提交回复
热议问题