java - swing, build and make GUI, empty screen -


i have swing program. first create frame , frame insert jpanel, contains graphical components. somehow, i'm not able draw bottom panel. have empty window.

    @override     public void run() {             frame.setdefaultcloseoperation(jframe.exit_on_close);             frame.add(new buildgui());             frame.pack();             frame.getcontentpane();             frame.setlocationrelativeto(null);                frame.setvisible(true);             frame.getcontentpane().validate();             frame.getcontentpane().repaint();     }      .     .     .       public class buildgui extends jpanel {         public void buildgui() {             bottompanel = new jpanel();              bottompanel.setlayout(new gridbaglayout());             // build gui here, add components bottompanel             frame.add(bottompanel);         }          @override         public dimension getpreferredsize() {             // works             return new dimension(800, 800);         }          @override         protected void paintcomponent(graphics g) {             // here want draw app gui             super.paintcomponent(g);         }    } 

thanks

i have feeling you're messing classes methods. lot of useless code seems have sneaked code.

the first point comes fact don't start class name capital. better class name be: buildgui, little bit dubious panel. next you're calling constructor of method frame.add(new buildgui()), won't call void buildgui() method.

the second point comes frame.getcontentpane() returns content pane of frame. because content pane isn't being stored, code nothing , can omitted.

my solution (staying close possible yours), follows:

@override public void run() {         mypanel panel = new mypanel();         panel.buildgui();         frame.add(panel);          frame.pack();         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true); }  ...  public class mypanel extends jpanel {      public void buildgui() {         bottompanel = new jpanel();          bottompanel.setlayout(new gridbaglayout());         // build gui here, add components bottompanel         this.add(bottompanel);     } } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -