visualizing JPanel change within a loop

后端 未结 3 968
感动是毒
感动是毒 2021-01-27 16:55

I am new to Java swing programming. I want to make a frame which will appear red and blue in turn one after another. So, I took 2 child JPanel, 1 for red and other for blue, and

3条回答
  •  借酒劲吻你
    2021-01-27 17:34

    Don't use a loop. Swing will only repaint the frame once the entire loop has finished executing.

    Instead you need to use a Swing Timer. When the Timer fires you invoke your logic. Read the section from the Swing tutorial on How to Use Swing Timers.

    Here is a simple example of a Timer that simply displays the time every second: Update a Label with a Swing Timer

    Also, don't remove/add panels. Instead you can use a Card Layout and sway the visible panel. Again read the tutorial on How to Use CardLayout.

提交回复
热议问题