Java Swing- adding a label below a single line bottom border in GridBagLayout -
i want label hotel management system copyright 2016 appear below matteborder
@ bottom , centered. have tried copyrightl.setverticalalignment(jlabel.bottom);
but doesn't seem work.
here code:
public class admin_hs extends jframe { jbutton bking_btn= new jbutton("bookings"); jbutton fd_btn= new jbutton("financial data"); jbutton ctm_btn= new jbutton("customers"); jbutton room_btn= new jbutton("rooms"); jbutton adc_btn= new jbutton("additional costs"); jbutton endb_btn= new jbutton("ending bookings"); //images jlabel bking_img= new jlabel(); jlabel fd_img= new jlabel(); jlabel ctm_img= new jlabel(); jlabel room_img= new jlabel(); jlabel adc_img= new jlabel(); jlabel endb_img= new jlabel(); jlabel copyrightl = new jlabel("hotel management system \u00a9 2016"); ///panels jpanel pnl1= new jpanel(); jpanel pnl= new jpanel((new flowlayout(flowlayout.left))); ///constructors public admin_hs(){ this.settitle("welcome admin!"); this.setlayout(new gridbaglayout()); ///setting layout pnl1.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth= gridbagconstraints.remainder; gbc.fill= gbc.horizontal; gbc.weightx = 1; // add line uses existing window size in x direction pnl1.add(pnl); // add line pnl.setlayout(new gridbaglayout()); gridbagconstraints gc= new gridbagconstraints(); ///first column of grid gc.insets = new insets(6, 6, 6, 6); gc.anchor = gridbagconstraints.west; gc.weightx = 0.5; gc.weighty = 0.5; gc.gridx = 0; gc.gridy = 0; pnl.add(bking_btn, gc); gc.gridx = 0; gc.gridy = 1; pnl.add(fd_btn, gc); gc.gridx = 0; gc.gridy = 2; pnl.add(ctm_btn, gc); gc.gridx = 0; gc.gridy = 3; pnl.add(room_btn, gc); gc.gridx = 0; gc.gridy = 4; pnl.add(adc_btn, gc); gc.gridx = 0; gc.gridy = 5; pnl.add(endb_btn, gc); gc.gridx = 0; gc.gridy = 8; pnl.add(copyrightl, gc); /////second column of grid gc.anchor = gridbagconstraints.west; gc.gridx = 1; gc.gridy = 0; bking_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/booking.jpg")); pnl.add(bking_img, gc); gc.gridx = 1; gc.gridy = 1; fd_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/fd.jpg")); pnl.add(fd_img, gc); gc.gridx = 1; gc.gridy = 2; ctm_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/guest.jpg")); pnl.add(ctm_img, gc); gc.gridx = 1; gc.gridy = 3; room_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/room.jpg")); pnl.add(room_img, gc); gc.gridx = 1; gc.gridy = 4; adc_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/adc.jpg")); pnl.add(adc_img, gc); gc.gridx = 1; gc.gridy = 5; endb_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/endb.png")); pnl.add(endb_img, gc); //copyrightl.sethorizontalalignment(jlabel.center); //copyrightl.setverticalalignment(jlabel.bottom); //pnl.add(copyrightl); pnl1.setborder(borderfactory.creatematteborder(0, 0, 1, 0, color.black)); this.add(pnl1,gbc); } }
main class
public class admin_main { public static void main(string[] args) { admin_hs adm= new admin_hs(); adm.setdefaultcloseoperation(jframe.exit_on_close); adm.pack(); adm.setvisible(true); adm.setsize(780,520); } }
here how looks:
edit:
it worked me in way:
public class admin_hs extends jframe { jbutton bking_btn= new jbutton("bookings"); jbutton fd_btn= new jbutton("financial data"); jbutton ctm_btn= new jbutton("customers"); jbutton room_btn= new jbutton("rooms"); jbutton adc_btn= new jbutton("additional costs"); jbutton endb_btn= new jbutton("ending bookings"); //images jlabel bking_img= new jlabel(); jlabel fd_img= new jlabel(); jlabel ctm_img= new jlabel(); jlabel room_img= new jlabel(); jlabel adc_img= new jlabel(); jlabel endb_img= new jlabel(); jlabel copyrightl = new jlabel("hotel management system \u00a9 2016"); ///panels jpanel pnl1= new jpanel(); jpanel pnl2= new jpanel(new gridbaglayout()); ///constructors public admin_hs(){ this.settitle("welcome admin!"); this.setlayout(new gridbaglayout()); ///setting layout pnl1.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.fill= gbc.horizontal; gridbagconstraints gc= new gridbagconstraints(); ///first column of grid gc.insets = new insets(6, 6, 6, 6); gc.anchor = gridbagconstraints.west; gc.weightx = 0.5; gc.weighty = 0.5; gc.gridx = 0; gc.gridy = 0; pnl1.add(bking_btn, gc); gc.gridx = 0; gc.gridy = 1; pnl1.add(fd_btn, gc); gc.gridx = 0; gc.gridy = 2; pnl1.add(ctm_btn, gc); gc.gridx = 0; gc.gridy = 3; pnl1.add(room_btn, gc); gc.gridx = 0; gc.gridy = 4; pnl1.add(adc_btn, gc); gc.gridx = 0; gc.gridy = 5; pnl1.add(endb_btn, gc); gc.gridx = 0; gc.gridy = 8; pnl1.add(copyrightl, gc); /////second column of grid gc.anchor = gridbagconstraints.west; gc.gridx = 1; gc.gridy = 0; bking_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/booking.jpg")); pnl1.add(bking_img, gc); gc.gridx = 1; gc.gridy = 1; fd_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/fd.jpg")); pnl1.add(fd_img, gc); gc.gridx = 1; gc.gridy = 2; ctm_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/guest.jpg")); pnl1.add(ctm_img, gc); gc.gridx = 1; gc.gridy = 3; room_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/room.jpg")); pnl1.add(room_img, gc); gc.gridx = 1; gc.gridy = 4; adc_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/adc.jpg")); pnl1.add(adc_img, gc); gc.gridx = 1; gc.gridy = 5; endb_img.seticon(new imageicon("c:/users/diksha/desktop/oosd assignment/icons/60-60/endb.png")); pnl1.add(endb_img, gc); pnl1.setborder(borderfactory.creatematteborder(0, 0, 1, 0, color.black)); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; pnl2.add(copyrightl, gbc); gbc.gridwidth = 1; gbc.weightx = 1.0; gbc.gridx = 0; gbc.gridy = 0; add(pnl1, gbc); gbc.gridx = 0; gbc.gridy = 1; add(pnl2, gbc); }
}
here how looks now:
the buttons have somehow shifted left. that's fine me had move them left reason. how can corresponding images left though? mean, how can the images closer buttons left?
i believe easiest way use code similar this
import javax.swing.*; import java.awt.*; public class test extends jframe { private jpanel testpanel = new jpanel(new gridbaglayout()), testpanel2 = new jpanel(new gridbaglayout()); private gridbagconstraints c = new gridbagconstraints(); private jlabel label1 = new jlabel("something"), label2 = new jlabel("something else"); test() { super("example frame"); setlayout(new gridbaglayout()); setdefaultcloseoperation(jframe.exit_on_close); buildgui(); c.weightx = 1.0; c.gridx = 0; c.gridy = 0; add(testpanel, c); c.gridx = 0; c.gridy = 1; add(testpanel2, c); pack(); setvisible(true); } private void buildgui() { c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 0; testpanel.add(label1, c); testpanel.setborder(borderfactory.creatematteborder(0, 0, 1, 0, color.black)); c.gridx = 0; c.gridy = 0; testpanel2.add(label2, c); } public static void main(string[] args) { new test(); } }
in code, there 2 jpanels, each jlabel on it. first panel has border on bottom , second panel placed underneath. can setting layout on jframe adopt idea work code.
with code, this.
admin_main
import javax.swing.*; public class admin_main { public static void main(string[] args) { admin_hs adm= new admin_hs(); adm.setdefaultcloseoperation(jframe.exit_on_close); adm.pack(); adm.setvisible(true); } }
admin_hs
import javax.swing.*; import java.awt.*; public class admin_hs extends jframe { jbutton bking_btn= new jbutton("bookings"); jbutton fd_btn= new jbutton("financial data"); jbutton ctm_btn= new jbutton("customers"); jbutton room_btn= new jbutton("rooms"); jbutton adc_btn= new jbutton("additional costs"); jbutton endb_btn= new jbutton("ending bookings"); //images jlabel bking_img= new jlabel(); jlabel fd_img= new jlabel(); jlabel ctm_img= new jlabel(); jlabel room_img= new jlabel(); jlabel adc_img= new jlabel(); jlabel endb_img= new jlabel(); jlabel copyrightl = new jlabel("hotel management system \u00a9 2016"); ///panels jpanel pnl1 = new jpanel(new gridbaglayout()), pnl2 = new jpanel(new gridbaglayout()); ///constructors public admin_hs(){ settitle("welcome admin!"); setlayout(new gridbaglayout()); ///setting layout pnl1.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.fill= gridbagconstraints.horizontal; gbc.insets = new insets(6, 6, 6, 6); gbc.weightx = 1; gbc.weighty = 1; gbc.gridx = 0; gbc.gridy = 0; pnl1.add(bking_btn, gbc); gbc.gridx = 0; gbc.gridy = 1; pnl1.add(fd_btn, gbc); gbc.gridx = 0; gbc.gridy = 2; pnl1.add(ctm_btn, gbc); gbc.gridx = 0; gbc.gridy = 3; pnl1.add(room_btn, gbc); gbc.gridx = 0; gbc.gridy = 4; pnl1.add(adc_btn, gbc); gbc.gridx = 0; gbc.gridy = 5; pnl1.add(endb_btn, gbc); /////second column of grid gbc.weightx = 0; gbc.fill= gridbagconstraints.both; imageicon icon = new imageicon("smiley.gif"); image image2 = icon.getimage().getscaledinstance(50, 50, 0); imageicon icon2 = new imageicon(image2); gbc.gridx = 1; gbc.gridy = 0; bking_img.seticon(icon2); pnl1.add(bking_img, gbc); gbc.gridx = 1; gbc.gridy = 1; fd_img.seticon(icon2); pnl1.add(fd_img, gbc); gbc.gridx = 1; gbc.gridy = 2; ctm_img.seticon(icon2); pnl1.add(ctm_img, gbc); gbc.gridx = 1; gbc.gridy = 3; room_img.seticon(icon2); pnl1.add(room_img, gbc); gbc.gridx = 1; gbc.gridy = 4; adc_img.seticon(icon2); pnl1.add(adc_img, gbc); gbc.gridx = 1; gbc.gridy = 5; endb_img.seticon(icon2); pnl1.add(endb_img, gbc); pnl1.setborder(borderfactory.creatematteborder(0, 0, 1, 0, color.black)); gbc.insets = new insets(0, 0, 0, 0); gbc.gridwidth = 1; gbc.weightx = 1.0; gbc.gridx = 0; gbc.gridy = 0; add(pnl1, gbc); gbc.fill= gridbagconstraints.horizontal; gbc.weightx = 0; gbc.weighty = 0; gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; pnl2.add(copyrightl, gbc); gbc.weightx = 0; gbc.gridx = 0; gbc.gridy = 1; add(pnl2, gbc); } }
this produces this
Comments
Post a Comment