java - JFrames method setVisible(true) or setVisible(false) not working in a loop. Frames are not displayed properly -
jframes not displaying in loop. code:-
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.event.windowadapter; import java.awt.event.windowevent; public class swingdemo { public static void main(string[] args) { swingutilities.invokelater(new runnable(){ public void run() { final jframe jfrm= new jframe("a simple swing application"); final jframe jfrm2= new jframe("a simple swing application 2"); jfrm.setsize(275,100); jfrm.setlocation(100,100); jfrm2.setlocation(50,50); jfrm2.setsize(275,100); jfrm.setdefaultcloseoperation(jframe.exit_on_close); jfrm2.setdefaultcloseoperation(jframe.exit_on_close); jlabel jlab = new jlabel("swing means powerful guis."); jfrm.add(jlab); jbutton button0= new jbutton("loop"); jfrm.add(button0); jfrm.setlayout(new flowlayout()); jlabel jlab2 = new jlabel("swing means powerful guis again"); jfrm2.add(jlab2); //jfrm2.setvisible(true); jfrm.setvisible(true); button0.addactionlistener(new actionlistener() { private boolean confirmaction; @override public void actionperformed(actionevent e) { confirmaction = true; if (confirmaction) { try { while(true) { jfrm.setvisible(false); jfrm2.setvisible(true); try{ thread.sleep(15000); } catch(interruptedexception ie) { system.out.println("nothing"); } jfrm2.setvisible(false); jfrm.setvisible(true); } } catch (throwable t) { t.printstacktrace(system.out); } } } }); } }); } }
if problem not updating correctly (this thought since didn't explain issue), should try include these methods in loop:
validate(); repaint();
if issue different, please inform us.
Comments
Post a Comment