user interface - Java GUI Layout Issues -


i'm trying make simple program in java requires 8 jlabels on top 1 jbutton directly below it. tried using boxlayout , flowlayout, happens jlabels disappear @ start of program. when button clicked, displayed properly, have manually resize window. explain i'm doing wrong? thanks!

public class programui {    private jbutton _jbutton;     private arraylist<jlabel> _jlabels;    private jframe _jframe;     private jpanel _top, _bottom;   public programui(){ _jframe = new jframe ();  _jframe.getcontentpane().setlayout(new boxlayout(_jframe.getcontentpane(), boxlayout.y_axis));  _top = new jpanel(); _jframe.add(_top);  _bottom = new jpanel(); _jframe.add(_bottom);  _top.setlayout(new flowlayout(flowlayout.left)); _bottom.setlayout(new flowlayout(flowlayout.left));  _jlabels = new arraylist<jlabel>(); (int i=0; i<8; i++) {     jlabel label = new jlabel();     _jlabels.add(label);      _top.add(label);     //...rest of code not relevant  }   _jbutton = new jbutton();       _bottom.add(_jbutton);  _jframe.pack(); _jframe.setvisible(true); _jframe.setdefaultcloseoperation (jframe.exit_on_close); }  

i don't recommend neither flowlayout nor boxlayout. boxlayout simplistic , non-portable. flowlayout not layout manager, it's joke.

i recommend use either built-in grouplayout or third-party miglayout. need dedicate time learning how create layout.

here example miglayout.

package com.zetcode;  import java.awt.eventqueue; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import net.miginfocom.swing.miglayout;  public class programui extends jframe {      public programui() {          initui();     }      private void initui() {          setlayout(new miglayout("nogrid"));          add(new jlabel("label"));         add(new jlabel("label"));         add(new jlabel("label"));         add(new jlabel("label"));         add(new jlabel("label"));         add(new jlabel("label"));         add(new jlabel("label"));         add(new jlabel("label"), "wrap");         add(new jbutton("button"));          pack();          settitle("miglayout example");         setlocationrelativeto(null);                 setdefaultcloseoperation(jframe.exit_on_close);     }      public static void main(string[] args) {          eventqueue.invokelater(() -> {             programui ex = new programui();             ex.setvisible(true);         });     } } 

screenshot:

exampe screenshot


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -