swing - Java: Moving JButtons vertically in a JPanel with BoxLayout -


in program, need have 3 buttons. using boxlayout in jpanel, , have managed move them dead center of screen. correct size , in correct horizontal position, want move them top of frame. should use this?

import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import java.awt.dimension; import java.awt.toolkit; import javax.swing.box; import javax.swing.boxlayout; import javax.swing.jbutton;  public class library extends jframe {      private jframe jf1;     private jpanel jp1;     private jbutton jb1;     private jbutton jb2;     private jbutton jb3;      public library() {         jf1 = new jframe("library");         jf1.setvisible(true);         jf1.setdefaultcloseoperation(jframe.exit_on_close);         jf1.setsize(1080, 900);          dimension dim = toolkit.getdefaulttoolkit().getscreensize();         jf1.setlocation(dim.width/2-jf1.getsize().width/2, dim.height/2-jf1.getsize().height/2);          jp1 = (jpanel) jf1.getcontentpane();         jp1.setlayout(new boxlayout(jp1, boxlayout.line_axis));          jb1 = new jbutton("genre");         jb1.setminimumsize(new dimension(140, 60));         jb1.setpreferredsize(new dimension(150, 60));         jb1.setmaximumsize(new dimension(150, 60));         jb2 = new jbutton("author");         jb2.setminimumsize(new dimension(140, 60));         jb2.setpreferredsize(new dimension(150, 60));         jb2.setmaximumsize(new dimension(150, 60));         jb3 = new jbutton("title");         jb3.setminimumsize(new dimension(140, 60));         jb3.setpreferredsize(new dimension(150, 60));         jb3.setmaximumsize(new dimension(150, 60));          jp1.add(box.createrigidarea(new dimension(300, 0)));         jp1.add(jb1);         jp1.add(box.createrigidarea(new dimension(20,0)));         jp1.add(jb2);         jp1.add(box.createrigidarea(new dimension(20,0)));         jp1.add(jb3);            }      public static void main(string[] args) {         library shoe = new library();     } } 

but want move them top of frame.

by default content pane of frame borderlayout keep layout , create panel buttons , add panel top of frame.

jpanel panel = new jpanel( new flowlayout() ); panel.add(button1) ... add(panel, borderlayout.page_start); 

read section swing tutorial on how use borderlayout more information , working examples.

i using boxlayout in jpanel,

however if want using boxlayout have tell boxlayout how align each component vertically. default value jbutton align in center of available space.

if want @ top need change each button:

jb1.setalignmenty(0.0f); 

also, don't hardcode "rigid area" @ start of panel. instead want use box.createhorizontalglue() @ start , end of panel. components adjust width of frame changed.

again, tutorial has section on how use boxlayout explain above.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -