如何在 Java 中设置标签(彩色文本)的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2966334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set the colour of a label (coloured text) in Java?
提问by Stefanos Kargas
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?
一个标签可以有两种不同的颜色吗?
For example here:
例如这里:
The "Text Color:"
to be black and the "Red"
to be red.
该"Text Color:"
是黑色的,该"Red"
是红色的。
采纳答案by aioobe
Sure. To set the foreground color, simply use label.setForeground(Color.RED)
.
当然。要设置前景色,只需使用label.setForeground(Color.RED)
.
For the two-color question: You could for instance use html in your label-text:
对于双色问题:例如,您可以在标签文本中使用 html:
frame.add(new JLabel("<html>Text color: <font color='red'>red</font></html>"));
produces
产生
Another solution is of course to use two separate JLabels, each of which has its foreground color.
另一种解决方案当然是使用两个单独的 JLabel,每个 JLabel 都有其前景色。
回答by Raven Dreamer
You can set the color of a JLabel by altering the foreground category:
您可以通过更改前景类别来设置 JLabel 的颜色:
JLabel title = new JLabel("I love stackoverflow!", JLabel.CENTER);
title.setForeground(Color.white);
As far as I know, the simplest way to create the two-color label you want is to simply make two labels, and make sure they get placed next to each other in the proper order.
据我所知,创建您想要的双色标签的最简单方法是简单地制作两个标签,并确保它们以正确的顺序彼此相邻放置。
回答by Roman
JLabel label = new JLabel ("Text Color: Red");
label.setForeground (Color.red);
this should work
这应该有效
回答by arcamax
Just wanted to add on to what @aioobementioned above...
只是想补充上面@aioobe提到的内容......
In that approach you use HTML to color code your text. Though this is one of the most frequently used ways to color code the label text, but is not the most efficient way to do it.... considering that fact that each label will lead to HTML being parsed, rendering, etc. If you have large UI forms to be displayed, every millisecond counts to give a good user experience.
在这种方法中,您可以使用 HTML 对文本进行颜色编码。虽然这是对标签文本进行颜色编码的最常用方法之一,但并不是最有效的方法......考虑到每个标签都会导致 HTML 被解析、呈现等的事实。如果你有要显示的大型 UI 表单,每一毫秒都很重要,以提供良好的用户体验。
You may want to go through the below and give it a try....
您可能想通过以下内容并尝试一下....
Jide OSS(located athttps://jide-oss.dev.java.net/) is a professional open source library with a really good amount of Swing components ready to use. They have a much improved version of JLabel named StyledLabel. That component solves your problem perfectly... See if their open source licensing applies to your product or not.
Jide OSS(位于https://jide-oss.dev.java.net/)是一个专业的开源库,有大量可供使用的 Swing 组件。他们有一个大大改进的 JLabel 版本,名为 StyledLabel。该组件完美地解决了您的问题……看看他们的开源许可是否适用于您的产品。
This component is very easy to use. If you want to see a demo of their Swing Components you can run their WebStart demo located at www.jidesoft.com(http://www.jidesoft.com/products/1.4/jide_demo.jnlp). All of their offerings are demo'd... and best part is that the StyledLabel is compared with JLabel (HTML and without) in terms of speed! :-)
该组件非常易于使用。如果您想查看他们的 Swing 组件的演示,您可以运行位于www.jidesoft.com( http://www.jidesoft.com/products/1.4/jide_demo.jnlp)的 WebStart 演示。他们所有的产品都经过演示......最好的部分是 StyledLabel 与 JLabel(HTML 和没有)在速度方面进行了比较!:-)
A screenshot of the perf test can be seen at (http://img267.imageshack.us/img267/9113/styledlabelperformance.png)
性能测试的截图可以在(http://img267.imageshack.us/img267/9113/styledlabelperformance.png)看到
回答by Wazim the ANDROID boy
object.setForeground(Color.green);
*any colour you wish *object being declared earlier
*你想要的任何颜色 *早先声明的对象
回答by Honza Zidek
One of the disadvantages of using HTML for labels is when you need to write a localizable program (which should work in several languages). You will have issues to change just the translatable text. Or you will have to put the whole HTML code into your translations which is very awkward, I would even say absurd :)
将 HTML 用于标签的缺点之一是您需要编写可本地化的程序(该程序应支持多种语言)。您将无法仅更改可翻译文本。或者您必须将整个 HTML 代码放入您的翻译中,这非常尴尬,我什至会说很荒谬:)
gui_en.properties:
gui_en.properties:
title.text=<html>Text color: <font color='red'>red</font></html>
gui_fr.properties:
gui_fr.properties:
title.text=<html>Couleur du texte: <font color='red'>rouge</font></html>
gui_ru.properties:
gui_ru.properties:
title.text=<html>Цвет текста: <font color='red'>красная</font></html>