简体   繁体   中英

Java: how do I change the color of a string?

I want to make some characters gray, and leave the rest black. How do I do that?

You have to give more context, like James Black said. If you're talking about the console, check the bottom of my my post. If you're using Swing and a JLabel, for example:

JLabel title = new JLabel("Want a Raise?", JLabel.CENTER);
title.setForeground(Color.white);

Source: http://www.leepoint.net/notes-java/GUI/components/10labels/jlabel.html

It's really gonna depend on the context though.

Edit : Apparently you can, with Enigma .

Text color is typically part of the way it's output not the string it self. So as Mike said what context are you talking about? Is this the console, a GUI or a JSP page?

If you're talking about the console we would also need to know the platform. A Windows console is different then a Unix console. This source forge project looks like it could help http://sourceforge.net/projects/jcons/

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