简体   繁体   English

更改JLabel中某些文本的颜色

[英]Changing color of some text in a JLabel

I have a JLabel with text in it, and I want to append another piece of text to it, but the latter will be of a different color than the former (eg red). 我有一个带有文本的JLabel,我想在其上附加另一段文字,但后者的颜色与前者不同(例如红色)。 I've tried: 我试过了:

statusLabel.setText(statusLabel.getText() +
  " <html><span style\"color: red\">" + message + "</span></html>");

But it doesn't work. 但它不起作用。 It just shows the HTML tags but does not render them. 它只显示HTML标记但不呈现它们。 Any suggestions? 有什么建议么? Is it possible to change the color of some of the text in a JLabel? 是否可以更改JLabel中某些文本的颜色?

Try this: 尝试这个:

setText("<html>Some text <font color='red'>some text in red</font></html>");

Or for you case you can build the string like this: 或者对于你的情况你可以像这样构建字符串:

statusLabel.setText(String.format("<html>%s<font color='red'>%s</font></html>", 
        statusLabel.getText(), message));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM