java - Aligning buttons to center in BoxLayout -
i trying set buttons center using box.createhorizontalstrut()
method. if use this.getwidth()/2
not work. how can center in frame.
code
package ch17; import java.awt.color; import java.awt.container; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; import javax.swing.border.titledborder; public class q17_1 extends jframe{ jbutton left = new jbutton("<="); jbutton right = new jbutton("=>"); jpanel p1 = new jpanel(); jradiobutton rb1 = new jradiobutton("red"); jradiobutton rb2 = new jradiobutton("yellow"); jradiobutton rb3 = new jradiobutton("white"); jradiobutton rb4 = new jradiobutton("gray"); jradiobutton rb5 = new jradiobutton("green"); jpanel p2 = new jpanel(); message m = new message("welcome java"); public q17_1(){ setlayout(new gridlayout(3,1)); p1.setborder(new titledborder("select message panel background")); buttongroup group = new buttongroup(); group.add(rb1);group.add(rb2);group.add(rb3);group.add(rb4);group.add(rb5); rb1.setmnemonic('r');rb2.setmnemonic('y');rb3.setmnemonic('w');rb4.setmnemonic('g'); rb5.setmnemonic('n'); p1.setlayout(new gridlayout(1,5,5,5)); p1.add(rb1);p1.add(rb2);p1.add(rb3);p1.add(rb4);p1.add(rb5); p2.setlayout(new boxlayout(p2,boxlayout.x_axis)); add(p1); add(m); p2.add(box.createhorizontalstrut(250)); p2.add(left); p2.add(box.createhorizontalstrut(5)); p2.add(right); add(p2); left.addactionlistener((actionevent) -> { m.moveleft(); repaint(); }); right.addactionlistener((actionevent)-> { m.moveright(); repaint(); }); rb1.addactionlistener(m); rb2.addactionlistener(m); rb3.addactionlistener(m); rb4.addactionlistener(m); rb5.addactionlistener(m); } public static void main(string[] args) { q17_1 frame = new q17_1(); frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setvisible(true); } }
i have tried, this.getwidth()/2
, p2.getwidth()/2
, etc. don't work , buttons still starting beginning of left side.
you use combination of borderlayout
, flowlayout
or gridbaglayout
for summary:
setlayout(new borderlayout()); //... p2.setlayout(new flowlayout()); add(p1, borderlayout.north); add(m); //... add(p2, borderlayout.south);
the reason might consider using borderlayout
of gridlayout
core layout give remaining space component in center
position. might not want, it's why i've used it.
both gridbaglayout
, flowlayout
layout it's containers around centre of container, gridbaglayout
doing vertically , horizontally, flowlayout
doing horizontally (by default)
import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.gridlayout; import javax.swing.box; import javax.swing.buttongroup; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jradiobutton; import javax.swing.border.titledborder; public class q17_1 extends jframe { jbutton left = new jbutton("<="); jbutton right = new jbutton("=>"); jpanel p1 = new jpanel(); jradiobutton rb1 = new jradiobutton("red"); jradiobutton rb2 = new jradiobutton("yellow"); jradiobutton rb3 = new jradiobutton("white"); jradiobutton rb4 = new jradiobutton("gray"); jradiobutton rb5 = new jradiobutton("green"); jpanel p2 = new jpanel(); jlabel m = new jlabel("welcome java"); // message m = new message("welcome java"); public q17_1() { setlayout(new borderlayout()); p1.setborder(new titledborder("select message panel background")); buttongroup group = new buttongroup(); group.add(rb1); group.add(rb2); group.add(rb3); group.add(rb4); group.add(rb5); rb1.setmnemonic('r'); rb2.setmnemonic('y'); rb3.setmnemonic('w'); rb4.setmnemonic('g'); rb5.setmnemonic('n'); p1.setlayout(new gridlayout(1, 5, 5, 5)); p1.add(rb1); p1.add(rb2); p1.add(rb3); p1.add(rb4); p1.add(rb5); p2.setlayout(new flowlayout()); add(p1, borderlayout.north); add(m); p2.add(left); p2.add(right); add(p2, borderlayout.south); left.addactionlistener((actionevent) -> { // m.moveleft(); // repaint(); }); right.addactionlistener((actionevent) -> { // m.moveright(); // repaint(); }); // rb1.addactionlistener(m); // rb2.addactionlistener(m); // rb3.addactionlistener(m); // rb4.addactionlistener(m); // rb5.addactionlistener(m); } public static void main(string[] args) { q17_1 frame = new q17_1(); frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setvisible(true); } }
but if want use boxlayout practice p2. should put arguments in order center buttons???
because container's size dynamic, use horizontal glue instead
p2.add(box.createhorizontalglue()); p2.add(left); p2.add(right); p2.add(box.createhorizontalglue());
Comments
Post a Comment