java - How do I get JFrame to display my panel? -


i learning use jframe can start building applications however, having great deal of trouble getting panel display components. code have created far practice , feel actual jframe library.

here code:

    import java.awt.dimension;     import java.awt.toolkit;     import javax.swing.*;      public class gui extends jframe {      public static void main(string[] args){         new gui();               }      /*      * constructor of gui. sets dimension,      * visibility, , positioning of frame relative      * users dimension of screen size.       */     public gui(){         //'this' in reference frame being created         this.setsize(500, 400);         //this.setlocationrelativeto(null);         this.setvisible(true);          toolkit tk = toolkit.getdefaulttoolkit();         dimension dim = tk.getscreensize();     //will hold height , width          //grabs height , width positioning         //of screen , centers window         int xpos = (dim.width /2) - (this.getwidth() / 2);         int ypos = (dim.height /2) - (this.getheight() / 2);          this.setlocation(xpos, ypos);          //this prevents resizing frame         //by default method true.         this.setresizable(false);          //sets how frame close         this.setdefaultcloseoperation(jframe.exit_on_close);          //sets title of frame         this.settitle("woot created frame!");          /*          * panel used hold of different          * labels , components within frame.          */         jpanel thepanel = new jpanel();          jlabel label1 =  new jlabel("tell me something");         label1.settooltiptext("click if need help");          thepanel.add(label1);          //creates button         jbutton button1 = new jbutton("ok");         button1.setborderpainted(true);         button1.setcontentareafilled(true);         button1.settooltiptext("this button");         thepanel.add(button1);          //creates text field         jtextfield txtfield = new jtextfield("type here", 15);          txtfield.settooltiptext("it's field");         //adds txtfield frame         thepanel.add(txtfield);          //adds thepanel frame created         this.add(thepanel);     }     }  

when run this, display window appears not see text field. when comment out text field, button , label appear should.

your problem call setvisible(true) before you've added components gui, gui doesn't show components added later.

recommendations:

  • avoid null layouts , setbounds(...)
  • don't call setvisible(true) until after adding components jframe.
  • avoid setsize(). let gui , components size themselves.

so alone fix problem:

public gui() {   this.setsize(500, 400);   // this.setvisible(true);    // .........    txtfield.settooltiptext("it's field");   thepanel.add(txtfield);   this.add(thepanel);    setvisible(true);  } 

e.g.,

import java.awt.dimension; import javax.swing.*;  public class gui2 extends jpanel {    private static final int pref_w = 500;    private static final int pref_h = 400;     public gui2() {       jlabel label1 = new jlabel("tell me something");       label1.settooltiptext("click if need help");        add(label1);       jbutton button1 = new jbutton("ok");       button1.setborderpainted(true);       button1.setcontentareafilled(true);       button1.settooltiptext("this button");       add(button1);       jtextfield txtfield = new jtextfield("type here", 15);        txtfield.settooltiptext("it's field");       add(txtfield);    }     @override    public dimension getpreferredsize() {       return new dimension(pref_w, pref_h);    }     private static void createandshowgui() {       gui2 mainpanel = new gui2();        jframe frame = new jframe("woot created frame!");       frame.setdefaultcloseoperation(jframe.exit_on_close);       frame.getcontentpane().add(mainpanel);       frame.setresizable(false);       frame.pack();       frame.setlocationrelativeto(null);       frame.setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -