[英]How to change the color of particular characters in JLabel?
How to change the color of particular characters in JLabel? 如何更改JLabel中特定字符的颜色? For instance, 例如,
JLabel lbl = new JLabel("My Text");
I would like that My
has blue color, while Text
has red color. 我希望My
有蓝色,而Text
有红色。 Is it possible? 可能吗?
是的,请查看如何在Swing组件中使用HTML 。
JLabel lbl = new JLabel("<html><font color=blue>My</font><font color=red>Text</font></html>");
Try with html: 试试html:
String labelText =
"<html><FONT COLOR=BLUE>My</FONT>" +
"<FONT COLOR=RED>Text</FONT></html>";
JLabel lbl = new JLabel(labelText);
您可以在JLabels中使用HTML,因此这应该有效:
JLabel lbl = new JLabel("<html><span style='color: blue;'>My</span> <span style='color: red;'>Text</span></html>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.