swing - Java GridBagLayout Aligning buttons -
i have problem gridbaglayout; i've 5 buttons , want have them in way:
i've tried different approaches no 1 works in correct way.
for example:
import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.box; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testgridbaglayout { protected void initui() { jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel southpanel = new jpanel(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = 2; gbc.gridy = 0; jbutton enterroom = new jbutton("enter room"); jbutton exitroom = new jbutton("exit room"); jbutton login = new jbutton("login"); jbutton logout = new jbutton("logout"); jbutton whoisin = new jbutton("who in"); gbc.gridx = 1; southpanel.add(enterroom, gbc); gbc.gridx = 5; southpanel.add(exitroom, gbc); gbc.gridy = 1; gbc.gridx = 0; southpanel.add(login, gbc); gbc.gridx = 3; southpanel.add(logout, gbc); gbc.gridx = 6; southpanel.add(whoisin, gbc); frame.add(southpanel); frame.pack(); frame.setvisible(true); } public static void main(string[] args) throws classnotfoundexception, instantiationexception, illegalaccessexception, unsupportedlookandfeelexception { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); swingutilities.invokelater(new runnable() { @override public void run() { new testgridbaglayout().initui(); } }); } }
appears: i'm not interested in other approaches (such gridlayout), i'd know i'm missing.
gridbaglayout can strange animal in cases. anyway, gridwidth works, if there actual component requires width within "spanned" column (for example, if gridx=0 , gridwidth=2, column 0 has component , "spanned" column column 1).
in case, column 2, 4 & 7 have no components, width set 0. additionnaly, column 5 gets width of 0, because column 6 provides enough witdth exit room button, in end result see.
now, not sure of kind of layout trying achieve (i saw screenshot, how should behave when panel collapses/expands in width?). find below, example comes bit closer describe (although don't find nice)
import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testgridbaglayout2 { protected void initui() { jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel southpanel = new jpanel(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridy = 0; jbutton enterroom = new jbutton("enter room"); jbutton exitroom = new jbutton("exit room"); jbutton login = new jbutton("login"); jbutton logout = new jbutton("logout"); jbutton whoisin = new jbutton("who in"); gbc.gridx = 0; gbc.weightx = 1.0; gbc.anchor = gridbagconstraints.east; southpanel.add(enterroom, gbc); gbc.anchor = gridbagconstraints.west; gbc.gridx = 2; southpanel.add(exitroom, gbc); gbc.gridy = 1; gbc.gridx = 0; southpanel.add(login, gbc); gbc.weightx = 0; gbc.gridx = 1; southpanel.add(logout, gbc); gbc.weightx = 1.0; gbc.anchor = gridbagconstraints.east; gbc.gridx = 2; southpanel.add(whoisin, gbc); frame.add(southpanel); frame.pack(); frame.setsize(frame.getwidth() * 4 / 3, frame.getheight()); frame.setminimumsize(frame.getsize()); frame.setvisible(true); } public static void main(string[] args) throws classnotfoundexception, instantiationexception, illegalaccessexception, unsupportedlookandfeelexception { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); swingutilities.invokelater(new runnable() { @override public void run() { new testgridbaglayout().initui(); } }); } }
Comments
Post a Comment