java - How to change JLabel's size when the font size is changed? -
i've jlabel. code jlabel follows.
panelmain = new jpanel(); panelmain.setlayout(null); panelmain.setpreferredsize(new java.awt.dimension(800, 600)); panelmain.addcomponentlistener(listen); panelmain.setborder(null); titlebar = new jlabel("hello world"); titlebar.setbounds(10, 10, 100, 30); panelmain.add(titlebar);
my questing if change font of titlebar
(i.e. jlabel), how change size (which set in code titlebar.setbounds(10, 10, 100, 30);
) of titlebar
?
edit girish
my full code below.
import java.awt.event.componentevent; import java.awt.event.componentlistener; import javax.swing.jinternalframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; public class iframe extends jinternalframe { /** * */ private static final long serialversionuid = 6526561589695424088l; private jscrollpane jsp; private iflisten listen; private jpanel panelmain; protected jpanel panel; private string title; private jlabel titlebar; public iframe() { this.title=""; init(); } public iframe(string title) { this.title=title; init(); } private void init() { setlayout(null); listen=new iflisten(); panelmain=new jpanel(); panelmain.setlayout(null); panelmain.setpreferredsize(new java.awt.dimension(800, 600)); panelmain.addcomponentlistener(listen); panelmain.setborder(null); titlebar=new jlabel("hello world"); titlebar.setfont(new java.awt.font("monotype corsiva", 1, 48)); panelmain.add(titlebar); panel=new jpanel(); panel.setborder(javax.swing.borderfactory.createtitledborder(title)); panel.setminimumsize(new java.awt.dimension(400, 400)); panel.setsize(400, 400); panelmain.add(panel); jsp=new jscrollpane(panelmain); jsp.setborder(null); add(jsp); this.addcomponentlistener(listen); } //info custom methods public void settitlefont(java.awt.font font) { titlebar.setfont(font); //here want change size of label. } //info listener class iframe private class iflisten implements componentlistener { //info overridden methods @override public void componentresized(componentevent e) { if(e.getsource() instanceof iframe) jsp.setbounds(5, 5, getwidth()-20, getheight()-20); else if(e.getsource()==panelmain) { panel.setlocation(integer.parseint(panelmain.getwidth()/2-panel.getwidth()/2+""), 0); } } //info unimplemented methods @override public void componentshown(componentevent arg0) {} @override public void componenthidden(componentevent arg0) {} @override public void componentmoved(componentevent arg0) {} } }
i've commented font size changed , want change size of jlabel.
don't use setpreferredsize
, you've removed calculations label uses calculate size be.
avoid using null
layouts, pixel perfect layouts illusion within modern ui design. there many factors affect individual size of components, none of can control. swing designed work layout managers @ core, discarding these lead no end of issues , problems spend more , more time trying rectify
make use of appropriate layouts. important thing here is, no 1 layout ever want. need learn take advantage of each layout's strengths (and weaknesses) , use them advantage. commonly known "compound layouts". have @ laying out components within container more details , ideas
import java.awt.borderlayout; import java.awt.dimension; import java.awt.eventqueue; import java.awt.font; import java.awt.gridbaglayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jslider; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener; 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 { private jlabel label; private jslider slider; public testpane() { label = new jlabel("look, no hands!"); setlayout(new borderlayout()); jpanel panel = new jpanel(new gridbaglayout()); panel.add(label); add(panel); slider = new jslider(8, 96); add(slider, borderlayout.south); slider.addchangelistener(new changelistener() { @override public void statechanged(changeevent e) { font font = label.getfont(); font = font.derivefont((float)slider.getvalue()); label.setfont(font); } }); slider.setvalue(8); } @override public dimension getpreferredsize() { return new dimension(400, 400); } } }
jlabel
surprising proficient, changing font (text/icon) automatically cause invalidate layout , request repaint itself...
Comments
Post a Comment