swing - Buttons doesn't fill all available space when I use GridBagLayout in Java -
i want create application buttons arrayed invisible table. buttons should fill available space within imaginary cells when i´ll resize frame. i´m using swing, gridbaglayout
. i've read articles , solution add together .weightx=1
, .weighty=1
. weightx
works perfect , fill gaps weighty doesn't. buttons don´t extend height of cell. there problem code or there add together solve problem? or should utilize absolutely layout?
public class newclass { public void newclass(){ jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new jpanel(); frame.add(panel); gridbaglayout layout = new gridbaglayout(); panel.setlayout(layout); gridbagconstraints gbc = new gridbagconstraints(); gbc.fill = gridbagconstraints.horizontal; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridx = 0; gbc.gridy = 0; jbutton b11 = new jbutton("11"); panel.add(b11,gbc); gbc.gridx = 1; gbc.gridy = 0; jbutton b12 = new jbutton("12"); panel.add(b12,gbc); gbc.gridx = 0; gbc.gridy = 1; jbutton b21 = new jbutton("21"); panel.add(b21,gbc); gbc.gridx = 1; gbc.gridy = 1; jbutton b22 = new jbutton("22"); panel.add(b22,gbc); frame.pack(); frame.setsize(800,400); frame.setvisible(true); }}
you're using wrong gridbagconstraints#fill
field value, since value you're using telling layout manager fill buttons horizontally only.
you need change
gbc.fill = gridbagconstraints.horizontal;
to
// gbc.fill = gridbagconstraints.horizontal; gbc.fill = gridbagconstraints.both;
if want buttons fill both directions, both horizontally , vertically.
java swing button layout-manager gridbaglayout
No comments:
Post a Comment