java - I can not figure out why my GridBagLayout is not following the rules -
i've been working on making styling work, can not figure out why gridbagconstraints supposed exist doesn't work. works degree, won't work... specific code here:
private void initcomponents() { usernamelabel = new jlabel(); schedule = new courselist(); addcoursebutton = new jbutton(); deletecoursebutton = new jbutton(); printschedulebutton = new jbutton(); feesbutton = new jbutton(); helplink = new swinglink("help", "http://java.sun.com"); logoutbutton = new jbutton(); string u = getusername(); gridbaglayout gridbag = new gridbaglayout(); gridbagconstraints gbc; //set layout this.setlayout(gridbag); //row 1 usernamelabel.settext("welcome " + u); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 6; gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new insets(20,10,10,10); //top, left, bottom, right gbc.anchor = gbc.line_end; gbc.fill = gbc.both; this.add(usernamelabel, gbc); //rows 2-4 schedule.getaccessiblecontext().setaccessiblename("course schedule"); gbc = new gridbagconstraints(); gbc.gridheight = 3; gbc.gridwidth = 6; gbc.gridx = 0; gbc.gridy = 1; gbc.insets = new insets(10,10,10,10); //top, left, bottom, right gbc.anchor = gbc.center; gbc.fill = gbc.both; this.add(schedule, gbc); //row 5 - empty label jlabel hiddenlabel1 = new jlabel(); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 6; gbc.gridx = 0; gbc.gridy = 4; gbc.insets = new insets(10,10,10,10); //top, left, bottom, right gbc.fill = gbc.both; this.add(hiddenlabel1, gbc); //row 6 addcoursebutton.settext("register"); addcoursebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent evt) { addcourseactionperformed(evt); } }); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 2; gbc.gridx = 0; gbc.gridy = 5; gbc.ipadx = 10; gbc.insets = new insets(10,10,10,5); //top, left, bottom, right gbc.anchor = gbc.center; gbc.fill = gbc.horizontal; this.add(addcoursebutton, gbc); deletecoursebutton.settext("drop delete course"); deletecoursebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent evt) { deletecourseactionperformed(evt); } }); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 2; gbc.gridx = 2; gbc.gridy = 5; gbc.ipadx = 10; gbc.insets = new insets(10,5,10,5); //top, left, bottom, right gbc.anchor = gbc.center; gbc.fill = gbc.horizontal; this.add(deletecoursebutton, gbc); printschedulebutton.settext("print schedule"); printschedulebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent evt) { printscheduleactionperformed(evt); } }); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 2; gbc.gridx = 4; gbc.gridy = 5; gbc.ipadx = 10; gbc.insets = new insets(10,5,10,10); //top, left, bottom, right gbc.anchor = gbc.center; gbc.fill = gbc.horizontal; this.add(printschedulebutton, gbc); //row 7 feesbutton.settext("fees"); feesbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent evt) { feesbuttonactionperformed(evt); } }); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 2; gbc.gridx = 0; gbc.gridy = 6; gbc.ipadx = 10; gbc.insets = new insets(10,10,20,5); //top, left, bottom, right gbc.anchor = gbc.line_start; gbc.fill = gbc.horizontal; this.add(feesbutton, gbc); //help link postioning gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 2; gbc.gridx = 2; gbc.gridy = 6; gbc.ipadx = 10; gbc.insets = new insets(10,5,20,5); //top, left, bottom, right gbc.anchor = gbc.center; gbc.fill = gbc.horizontal; helplink.setcursor(new cursor(cursor.hand_cursor)); this.add(helplink, gbc); logoutbutton.settext("logout"); logoutbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent evt) { logoutbuttonactionperformed(evt); } }); gbc = new gridbagconstraints(); gbc.gridheight = 1; gbc.gridwidth = 2; gbc.gridx = 4; gbc.gridy = 6; gbc.ipadx = 10; gbc.insets = new insets(10,5,20,10); //top, left, bottom, right gbc.anchor = gbc.line_end; gbc.fill = gbc.horizontal; this.add(logoutbutton, gbc); setborder(borderfactory.createtitledborder(new matteborder(null), "", titledborder.center, titledborder.top, new font("tahoma", 1, 14), new color(255, 255, 255))); // noi18n getaccessiblecontext().setaccessiblename("student panel welcome"); setopaque(false); }
when run layout gets drawn this: code running written
should centered , more akin this: design guide wrote on piece of paper in meeting of sorts
if other code necessary solve problem, let me know. i'm sure stupid error on part, can not life of me find errors. , sorry if trivial question, it's been bugging me forever, , need move on other parts of gui.
gridwidth
can difficult thing work with, better approach might separate layout components , focus on individual layout requirements separately
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablemodel; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { public testpane() { setlayout(new borderlayout()); jlabel title = new jlabel("welcome"); title.sethorizontalalignment(jlabel.center); add(title, borderlayout.north); tablemodel model = new defaulttablemodel(new object[]{"", "crn", "department", "class", "time", "place"}, 5); add(new jscrollpane(new jtable(model))); jpanel options = new jpanel(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new insets(4, 4, 4, 4); gbc.anchor = gridbagconstraints.center; gbc.fill = gridbagconstraints.horizontal; options.add(new jbutton("add course"), gbc); gbc.gridx++; options.add(new jbutton("delete course"), gbc); gbc.gridx++; options.add(new jbutton("print schedule"), gbc); gbc.gridx = 0; gbc.gridy++; options.add(new jbutton("fees"), gbc); gbc.gridx++; jlabel = new jlabel("help"); help.sethorizontalalignment(jlabel.center); options.add(help, gbc); gbc.gridx++; options.add(new jbutton("logout"), gbc); add(options, borderlayout.south); } } }
to reduce clutter of ui, might consider using jtoolbar
instead of buttons along bottom edge, see how use tool bars more details
Comments
Post a Comment