如何在Java中设置标签(彩色文本)的颜色?

问题描述:

如何设置标签文本的颜色?

How do I set the color of the text of a label?

myLabel.setText("Text Color: Red");
myLabel.???

我可以在一个标签中使用两种不同的颜色吗?

Can I have two seperate colors in one label?

例如此处:

"Text Color:"为黑色,"Red"为红色.

好的.要设置前景色,只需使用label.setForeground(Color.RED).

Sure. To set the foreground color, simply use label.setForeground(Color.RED).

关于双色问题:例如,您可以在标签文本中使用html:

For the two-color question: You could for instance use html in your label-text:

frame.add(new JLabel("<html>Text color: <font color='red'>red</font></html>"));

产生

另一种解决方案当然是使用两个单独的JLabel,每个JLabel都有其前景色.

Another solution is of course to use two separate JLabels, each of which has its foreground color.