java - Weird behavior of GridBagLayout -
i've discovered strange thing in code:
import java.awt.color; import java.awt.container; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.io.ioexception; import javax.swing.borderfactory; import javax.swing.box; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.swingutilities; public class test { public static void main(string [] args) throws ioexception { final jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new jpanel(); panel.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.insets.top = 5; gbc.insets.right = 5; add(0, 0, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(2, 0, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(0, 1, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(2, 1, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(0, 2, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(1, 2, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(3, 2, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(0, 3, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(1, 3, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(3, 3, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(0, 4, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(1, 4, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(3, 4, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); add(0, 5, 1, 1, 4, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, panel, createpanel()); frame.setcontentpane(panel); swingutilities.invokelater(new runnable() { @override public void run() { frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } private static void add(int gridx, int gridy, int weightx, int weighty, int gridw, int gridh, int fill, int anchor, gridbagconstraints gbc, container container, jcomponent component) { gbc.gridx = gridx; gbc.gridy = gridy; gbc.weightx = weightx; gbc.weighty = weighty; gbc.gridwidth = gridw; gbc.gridheight = gridh; gbc.fill = fill; gbc.anchor = anchor; component.add(box.createhorizontalstrut(5)); component.add(new jlabel("(" + gridx + "; " + gridy + ")")); component.add(box.createhorizontalstrut(5)); component.add(new jlabel("gridwidth = " + gridw)); component.add(box.createhorizontalstrut(5)); container.add(component, gbc); } private static jpanel createpanel() { jpanel panel1 = new jpanel(); panel1.setbackground(color.white); panel1.setborder(borderfactory.createlineborder(color.red)); return panel1; } }
and how looks on run:
there coordinates in brackets , gridwidth of each panel. can see last row, global gridwidth 4. rows 3-5 (y=2, y=3, y=4) in column 1 (x=0) have gridwidth equal 1. in first 2 rows first column has gridwidth equal 2, , supposed divided in middle of frame, isn't. why?
what found myself:
public class test { public static void main(string [] args) throws ioexception { final jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new jpanel(); panel.setlayout(new boxlayout(panel, boxlayout.y_axis)); gridbagconstraints gbc = new gridbagconstraints(); gbc.insets.top = 5; gbc.insets.right = 5; jpanel toppanel = new jpanel(); toppanel.setlayout(new gridbaglayout()); jpanel bottompanel = new jpanel(); bottompanel.setlayout(new gridbaglayout()); add(0, 0, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, toppanel, createpanel()); add(1, 0, 1, 1, 2, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, toppanel, createpanel()); add(0, 0, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, bottompanel, createpanel()); add(1, 0, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, bottompanel, createpanel()); add(2, 0, 1, 1, 1, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, bottompanel, createpanel()); add(0, 2, 1, 1, 3, 1, gridbagconstraints.both, gridbagconstraints.center, gbc, bottompanel, createpanel()); panel.add(toppanel); panel.add(bottompanel); frame.setcontentpane(panel); swingutilities.invokelater(new runnable() { @override public void run() { frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } private static void add(int gridx, int gridy, int weightx, int weighty, int gridw, int gridh, int fill, int anchor, gridbagconstraints gbc, container container, jcomponent component) { gbc.gridx = gridx; gbc.gridy = gridy; gbc.weightx = weightx; gbc.weighty = weighty; gbc.gridwidth = gridw; gbc.gridheight = gridh; gbc.fill = fill; gbc.anchor = anchor; component.add(box.createhorizontalstrut(5)); component.add(new jlabel("(" + gridx + "; " + gridy + ")")); component.add(box.createhorizontalstrut(5)); component.add(new jlabel("gridwidth = " + gridw)); component.add(box.createhorizontalstrut(5)); container.add(component, gbc); } private static jpanel createpanel() { jpanel panel1 = new jpanel(); panel1.setbackground(color.white); panel1.setborder(borderfactory.createlineborder(color.red)); return panel1; } }
result looks like:
what i've done: divided content pane 2 separate panels - top (with single row has 2 equally divided cells) , bottom (with 2 rows: 1 has 3 cells , has 1 single wide cell). content pane has boxlayout instead of gbl, , works expected. i'm disappointed gridbaglayout, perfect layout me before now.
Comments
Post a Comment