简体   繁体   中英

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.

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...)

You could use html in your label text as follows:

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 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). 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 :)

gui_en.properties:

title.text=<html>Text color: <font color='red'>red</font></html>

gui_fr.properties:

title.text=<html>Couleur du texte: <font color='red'>rouge</font></html>

gui_ru.properties:

title.text=<html>Цвет текста: <font color='red'>красная</font></html>

Just wanted to add on to what @aioobe mentioned above...

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.

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. 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.

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! :-)

A screenshot of the perf test can be seen at ( http://img267.imageshack.us/img267/9113/styledlabelperformance.png )

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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