简体   繁体   English

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

[英]How do I set the colour of a label (coloured text) in 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?我可以在一个标签中使用两种不同的颜色吗?

For example here:例如这里:

The "Text Color:" to be black and the "Red" to be red. "Text Color:"为黑色, "Red"为红色。

For single color foreground color对于单色前景色

label.setForeground(Color.RED)

For multiple foreground colors in the same label:对于同一标签中的多个前景色:

(I would probably put two labels next to each other using a GridLayout or something, but here goes...) (我可能会使用GridLayout或其他东西将两个标签并排放置,但这里是......)

You could use html in your label text as follows:您可以在标签文本中使用 html,如下所示:

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

which produces:产生:

在此处输入图像描述

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.据我所知,创建所需的双色标签的最简单方法是简单地制作两个标签,并确保它们以正确的顺序彼此相邻放置。

JLabel label = new JLabel ("Text Color: Red");
label.setForeground (Color.red);

this should work这应该工作

object.setForeground(Color.green);

*任何你想要的颜色 *对象被提前声明

One of the disadvantages of using HTML for labels is when you need to write a localizable program (which should work in several languages).使用 HTML 作为标签的缺点之一是当您需要编写一个可本地化的程序(它应该可以在多种语言中工作)时。 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 代码放入您的翻译中,这非常尴尬,我什至会说很荒谬:)

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>

Just wanted to add on to what @aioobe mentioned above...只是想补充上面提到的@aioobe ...

In that approach you use HTML to color code your text.在这种方法中,您使用 HTML 对文本进行颜色编码。 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 被解析、呈现等这一事实。如果你有要显示的大型 UI 表单,每一毫秒都很重要,以提供良好的用户体验。

You may want to go through the below and give it a try....您可能想通过以下内容尝试一下....

Jide OSS ( located at https://jide-oss.dev.java.net/ ) is a professional open source library with a really good amount of Swing components ready to use. Jide OSS位于https://jide-oss.dev.java.net/ )是一个专业的开源库,拥有大量可供使用的 Swing 组件。 They have a much improved version of JLabel named StyledLabel.他们有一个名为 StyledLabel 的 JLabel 的改进版本。 That component solves your problem perfectly... See if their open source licensing applies to your product or not.该组件完美地解决了您的问题......看看他们的开源许可是否适用于您的产品。

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 ).如果您想查看他们的 Swing 组件的演示,您可以运行位于www.jidesoft.com ( http://www.jidesoft.com/products/1.4/jide_demo.jnlp ) 的 WebStart 演示。 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!他们所有的产品都经过演示......最好的部分是 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 ) 看到

myLabel.setForeground(new java.awt.Color(255, 0, 0));

括号中的数字描述了红、绿、蓝颜色值的组合,值越高,颜色越浅,值可以在 0 到 255 之间变化。

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

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