swing - while loop ignore the event listener -
So when I try to change the background of the GUI crash to run this code and gets trapped in an infinite , Listeners. Here's the code:
private panel getPanel1 () {if (panel 1 == blank) {panel1 = new panel (); Panel1.setLayout (new gridoutout ()); While (frame.isVisible ()) {panel1.addMouseListener (new Java.wt.event.MouseAdapter () {public void mouseClicked (java.awt.event.MouseEvent e) {frame.setVisible (false);}}) ; Int r = (int) (mathematics. Randem () * 255); Int g = (int) (monastery. Rhythm () * 255); Int b = (int) (Math.random () * 255); Color C = new color (R, G, B); Panel1.setBackground (c); Try {Thread.sleep (4000); } Grip (Interrupted e1) {e1.printStackTrace (); } Panel 1.addMouseListener (new java.awt.event.MouseAdapter () {public void mouseClicked (java.awt.event.MouseEvent e) {/=panel1.setVisible(false); frame.acceptive (false); * / system Exit (0);}}); }} Return panel 1; }
Rather than ending the program or ending the background change event, it displays the panel and does nothing and I have to force it to leave me should do?
You can effectively sleep
in a loop in that loop But also add two listeners, which are quite bizarre.
Do not block UI formulas. Take care to give events to GUI structures etc. Basically you need to take an incident-based approach to the UI, instead you are taking the from the perspective that will never allow any event to be transmitted (as you call it anytime But the controls are not returning).
Create a panel, add the appropriate event listener, and then return it to the caller. If you want to change the background color in every 4 seconds, then you should do this so that it will stop the UI thread for 4 seconds. Could not.
Comments
Post a Comment