java - JPanel not showing, tried doing things like invokeLater, still not showing -


i've been having few problems jpanel not showing up(probably because of way threaded in swing?). i've tried re-ordering it's added, adding swingutilities.invokelater(...); , still haven't worked. searching on google, of them use invokelater function, doesn't work me. i'd assume wouldn't need layout manager, since sizes , positions done .setbounds()? code i've got @ moment

public class menulogin extends jpanel{  private jtextfield txtusername; private jpasswordfield txtpassword;   public menulogin(){      setbounds(0, 0, 380, 205);     setbackground(color.white);     swingutilities.invokelater(new runnable() {         public void run() {             jlabel lbllogin = new jlabel("login");             lbllogin.setbounds(155, 5, 85, 42);             lbllogin.setfont(new font("trebuchet ms", font.plain, 36));             lbllogin.sethorizontalalignment(swingconstants.center);               jpanel form = new jpanel(); // panel won't show             form.setborder(null);             form.setbounds(10, 74, 390, 195);             add(form);             form.setlayout(null);             form.setvisible(true);              txtusername = new jtextfield();             txtusername.setfont(new font("trebuchet ms", font.plain, 17));             txtusername.settooltiptext("username");             txtusername.setbounds(10, 30, 370, 30);             txtusername.setcolumns(16);             form.add(txtusername);              txtpassword = new jpasswordfield();             txtpassword.setfont(new font("trebuchet ms", font.plain, 17));             txtpassword.settooltiptext("password");             txtpassword.setcolumns(16);             txtpassword.setbounds(10, 78, 370, 30);             form.add(txtpassword);              jbutton btnproceed = new jbutton("proceed");             btnproceed.setfont(new font("trebuchet ms", font.plain, 15));             btnproceed.setbounds(150, 119, 94, 30);             form.add(btnproceed);              jbutton btnplayasguest = new jbutton("play guest");             btnplayasguest.setfont(new font("trebuchet ms", font.plain, 9));             btnplayasguest.setbounds(291, 161, 89, 23);             form.add(btnplayasguest);              jbutton btnsignup = new jbutton("sign up");             btnsignup.setfont(new font("trebuchet ms", font.plain, 13));             btnsignup.setbounds(155, 160, 83, 23);             form.add(btnsignup);             add(lbllogin);         }       }); } } 

the thing is, first jlabel shown... only jlabel shows...

any suggestions why still not showing nice. thanks.

this should work:

import java.awt.borderlayout; import java.awt.color; import java.awt.font;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jpasswordfield; import javax.swing.jtextfield; import javax.swing.swingutilities;  public class menulogin extends jpanel {      private jtextfield txtusername;     private jpasswordfield txtpassword;      public menulogin() {         setlayout(new borderlayout());         setbackground(color.white);          jpanel form = new jpanel(); // panel won't show         form.setvisible(true);          jlabel lbllogin = new jlabel("login");         lbllogin.setfont(new font("trebuchet ms", font.plain, 36));         form.add(lbllogin);          txtusername = new jtextfield();         txtusername.setfont(new font("trebuchet ms", font.plain, 17));         txtusername.settooltiptext("username");         txtusername.setcolumns(16);         form.add(txtusername);          txtpassword = new jpasswordfield();         txtpassword.setfont(new font("trebuchet ms", font.plain, 17));         txtpassword.settooltiptext("password");         txtpassword.setcolumns(16);         form.add(txtpassword);          jbutton btnproceed = new jbutton("proceed");         btnproceed.setfont(new font("trebuchet ms", font.plain, 15));         form.add(btnproceed);          jbutton btnplayasguest = new jbutton("play guest");         btnplayasguest.setfont(new font("trebuchet ms", font.plain, 9));         form.add(btnplayasguest);          jbutton btnsignup = new jbutton("sign up");         btnsignup.setfont(new font("trebuchet ms", font.plain, 13));         form.add(btnsignup);          add(form, borderlayout.center);      }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {              @override             public void run() {                 jframe frame = new jframe();                 frame.setdefaultcloseoperation(jframe.dispose_on_close);                 frame.add(new menulogin());                 frame.pack();                 frame.setvisible(true);             }         });     } } 

i didn't try find 1 of setbounds methods caused problem (since never use them, , shouldn't too). removed setbounds methods , implemented suitable layout manager (flowlayout - primitive).

when comes advanced layouts, prefer miglayout because of simplicity. also, have @ gridbaglayout.

regards.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -