Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个带有文本的 JLabel,我想在其中添加另一段文本,但后者的颜色与前者的颜色不同(例如红色)。我试过了:
statusLabel.setText(statusLabel.getText() + " <html><span style\"color: red\">" + message + "</span></html>");
但它不起作用。它只显示 HTML 标签,但不呈现它们。有什么建议么?是否可以更改 JLabel 中某些文本的颜色?
试试这个:
setText("<html>Some text <font color='red'>some text in red</font></html>");
或者对于您的情况,您可以像这样构建字符串:
statusLabel.setText(String.format("<html>%s<font color='red'>%s</font></html>", statusLabel.getText(), message));