java - How to set JButton size with GridBagLayout -
i want set size of jbuttons @ center of screen become larger can't seem find how using gridbaglayouts.
here how looks :
here code :
// client c.fill = gridbagconstraints.both; c.anchor = gridbagconstraints.center; c.gridy = 5; c.gridx = 5; c.gridwidth = 1; c.gridheight = 1; c.insets = new insets(10, 1, 1, 10); p.add(b[0], c); // server c.fill = gridbagconstraints.both; c.anchor = gridbagconstraints.center; c.gridy = 10; c.gridx = 5; c.gridwidth = 1; c.gridheight = 1; c.insets = new insets(10, 1, 1, 10); p.add(b[1], c);
i want buttons take larger portion of empty space around them.
more information added: buttons have 50% of width , [about] 20% of height of parent [together 50% height including space in between]. (slightly rewritten match suggestion.)
solution
combination of simple layouts layouts. although if have 3 columns or 3 rows can't joined, rest can changed later:
// row variation jpanel parent = new jpanel(); parent.setlayout(new gridlayout(3, 1)); parent.add(new jpanel()); // placeholder 1st row jpanel row = new jpanel(); // 2nd row row.setlayout(new gridlayout(1, 3)); // create 3 cells of equal size row.add(new jpanel()); // 2nd row, 1st cell placeholder // have 33% x 33% (oops) rectangle in middle jpanel controls = new jpanel(); controls.setlayout(new gridlayout(2, 1, 10, 10)); controls.setborder(borderfactory.createemptyborder(10, 10, 10, 10); controls.add(new jbutton("client")); controls.add(new jbutton("server")); row.add(controls); // add 2nd row, 2nd cell row.add(new jpanel()); // 2nd row, 3rd cell placeholder parent.add(row); // add 2nd row parent.add(new jpanel()); // placeholder 3rd row
easy, won't able join cells later:
jpanel parent = new jpanel(); parent.setlayout(newgridlayout(9, 9));
bottom line: combine different layout managers, put 2 buttons inside panel , put placeholders inside, should work fine gridbaglayout. said, try stay flexible writing reusable components can combined layout manager. don't have use placeholders superfluous code in order display components correctly.
old answer
alternative solution: use boxlayout
boxlayout more intuitive , easier understand when looking @ code (of course opinion).
decide how window structered (is more big horizontal components on top of each other
page_axis
or big vertical components next each otherline_axis
) , use outer boxlayout:jpanel content = new jpanel(); // or frame content.setlayout(new boxlayout(content, boxlayout.line_axis));
add components along axis, have more 1 component along other axis use 2nd boxlayout. can space components creating rigid areas (empty rectangles having same size) or adding glue (expanding gum components).
content.add(boxlayout.createhorizntalglue()); jpanel col = new jpanel(); col.setlayout(new boxlayout(col, boxlayout.page_axis)); jbutton clientbtn = new jbutton("client"); jbutton serverbtn = new jbutton("server"); col.add(boxlayout.createverticalglue()); col.add(clientbtn); col.add(boxlayout.createrigidarea(new dimension(1, 10))); col.add(serverbtn); col.add(boxlayout.createverticalglue()); content.add(col); content.add(boxlayout.createhorizontalglue());
Comments
Post a Comment