The program freezes when i put a while loop in a action listener

前端 未结 1 589
醉酒成梦
醉酒成梦 2021-01-29 14:13

The program freezes because I put a loop in the action listener for the grandma button.I know why it won\'t work, but I can\'t seem to find a way around it. Can anyone tell me a

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 14:59

    In this program, there is a cookie button that works, and a grandma button that should trigger a while loop, that will add a cookie every 5 seconds.::

    Don't use a loop in the ActionListener. The loop blocks the Event Dispatch Thread and prevents the GUI from repainting itself.

    Instead you should be using a Swing Timer

    Read the sections from the Swing Tutorial on:

    1. Concurrency in Swing
    2. How to Use Timers

    for more information.

    Also, variable names should NOT start with an upper case character. Sometimes your names are correct and sometimes they are not. Be consistent!!!

    And get rid of all that blank space. Use proper formatting if you want to post code for people to read. Right now the posted code is a mess to read.

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