java - Change font of a part of a JLabel? -


i want change only first 2 words of jlabel different font rest of jlabel. have found make jpanel, , have 2 jlabels different fonts in it. cannot use way, because can have 1 jlabel (this since have mouse listener changes text of jlabel based on entrance or exit of different other jlabels, in seperate jpanel). there way? have tried (adding jlabel side side jlabel):

jlabel giraffesays = new jlabel("giraffe says:");         giraffesays.setfont(new font("timesroman", font.bold, 60));          status.settext(giraffesays +"hi!"); //status jlabel 

but didn't work. tried making string:

string giraffesays = "giraffe says:         giraffesays.setfont(new font("timesroman", font.bold, 60));          status.settext(giraffesays +"hi!"); //status jlabel 

but cannot change font of string...

try using html string jlabel:

import javax.swing.swingutilities; import javax.swing.jframe; import javax.swing.jlabel;  public class test {   public static void main(string[] args) {     swingutilities.invokelater(new runnable(){       @override public void run() {         jframe frm = new jframe("text formatting");         frm.setdefaultcloseoperation(jframe.dispose_on_close);         string giraffesays = "<html><span style=\"font-family:arial;font-size:13px;\">giraffe says :</span>hi there!</html>";         frm.getcontentpane().add(new jlabel(giraffesays));         frm.pack();         frm.setlocationbyplatform(true);         frm.setvisible(true);       }     });   } } 

enter image description here

this line error

string giraffesays = "<html><font size="6"><span style=\"font-family:arial;\">giraffe says :</font></span></html>"; 

problem need escape quotes size=\"6\".


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -