java - Component disappears randomly in the SOUTH position when using BorderLayout and pack() -


i have rather mysterious problem when using borderlayout. when place jbutton in center position , component (say jlabel) in south, latter 1 disappears randomly. here example. see problem you'll have resize window larger size , move mouse cursor on screen bit (on off button, label, window title bar, windows taskbar, edges of screen). don't need press button. after few seconds of such movements label disappears. reappears if resize or minimize/restore window again.

the strange thing is, pack() seems important, don't observe problem in use e.g. setsize(600,300) on jframe instead. seems center component must jbutton, while south component can (i tried jlabel, jcheckbox), , positions (south , center) important. not happen if button placed north, example.

it easy avoid situation in practice, of course, still know why happens, , whether did wrong or unsafe. using jdk 8 release 77 on windows 10.

import java.awt.*; import java.awt.event.*; import javax.swing.*;    public class myexample extends jframe{  myexample() { initgui();} // constructor   private void initgui(){   // create button   jbutton mybutton=new jbutton("press me !");     // create label   jlabel testlabel=new jlabel("test !!!");    // set window    jpanel pane=(jpanel) getcontentpane();   pane.setlayout(new borderlayout());    pane.add(mybutton,borderlayout.center);   pane.add(testlabel,borderlayout.south);     setdefaultcloseoperation(exit_on_close);   pack();      // setsize(600,300);     }   public static void main(string[] args){   eventqueue.invokelater(()->{    myexample me=new myexample();    me.setvisible(true);   });    } } 

note: defined jpanel object pane clarity, if write instead

  // set window    add(mybutton,borderlayout.center);   add(testlabel,borderlayout.south);  

the result same.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -