swing - Java: What Layout Manager would be best for a game menu? -


===================
game name

play
exit

===================

the above previous game menu looked like. used box layout create tedious. there there better layout manager use?

here code asked of main pane.

private jbutton jb; private jbutton eb; private joptionpane jo;  public startupwindow(){     super("pong");      jpanel outside = new jpanel();     jpanel inside = new jpanel();     setlayout(new borderlayout());        outside.setlayout(new boxlayout(outside, boxlayout.line_axis));     inside.setlayout(new boxlayout(inside, boxlayout.page_axis));      outside.add(box.createhorizontalstrut(280));     outside.add(inside);     outside.add(box.createhorizontalstrut(20));      inside.add(box.createverticalstrut(20));     jlabel title = new jlabel("      "+"pong");     title.setfont( new font("serif", font.bold, 40));     inside.add(title);     inside.add(box.createverticalstrut(20));      jbutton btt1 = new jbutton("start");     dimension d = new dimension(200,40);      btt1.setsize(d);     btt1.setminimumsize(d);     btt1.setmaximumsize(d);     btt1.setpreferredsize(d);      jbutton btt2 = new jbutton("credits");     btt2.setsize(d);     btt2.setminimumsize(d);     btt2.setmaximumsize(d);     btt2.setpreferredsize(d);     jbutton btt3 = new jbutton("exit");     btt3.setsize(d);     btt3.setminimumsize(d);     btt3.setmaximumsize(d);     btt3.setpreferredsize(d);      inside.add(btt1);     btt1.addactionlistener(this);     btt1.setactioncommand("start");      inside.add(box.createverticalstrut(5));      inside.add(btt2);     btt2.addactionlistener(this);     btt2.setactioncommand("credits");      inside.add(box.createverticalstrut(5));      inside.add(btt3);     btt3.addactionlistener(this);     btt3.setactioncommand("exit");      inside.add(box.createverticalstrut(20));      add(outside);      this.setdefaultcloseoperation(jframe.exit_on_close);     this.setsize(800,600);     this.setvisible(true);     this.setresizable(false);     this.setlocation(450,200);      inside.setbackground(color.gray);     outside.setbackground(color.gray);   } 

i agree boxlayout tedious admire relative simplicity.

another quick , easy option use "javax.swing.box" class instead of using layout manager directly.

box box = box.createverticalbox(); box.add(new jlabel("game")); box.add(box.createverticalstrut(20)); box.add(new jlabel("button 1")); box.add(new jlabel("button 2"));  jframe frame = new jframe(); frame.add(box); frame.pack(); frame.setvisible(true); 

box offers number of useful methods. can use create vertical , horizontal boxes, create "struts" reserve horizontal , vertical space, , create "glue" fill in available space when layout grows.

of course use gridbaglayout, tend reserve more complex layouts. box , cousin boxlayout enough simple layouts , easy new programmers maintaining application understand , debug.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -