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); } }); } }
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
Post a Comment