在Java编程中,输出颜色文字是一个常见的需求,尤其是在开发命令行工具或者创建一些简单的文本界面时,以下是一些方法,可以帮助你让Java输出颜色文字。

使用ANSI转义序列
ANSI转义序列是一种广泛支持的文本颜色和样式标准,许多终端和命令行界面都支持这些序列,以下是一个简单的例子,展示如何使用ANSI转义序列来输出不同颜色的文字:
public class ColorTextExample {
public static void main(String[] args) {
System.out.println("\033[0;31mThis is red text\033[0m");
System.out.println("\033[0;32mThis is green text\033[0m");
System.out.println("\033[0;33mThis is yellow text\033[0m");
System.out.println("\033[0;34mThis is blue text\033[0m");
System.out.println("\033[0;35mThis is purple text\033[0m");
System.out.println("\033[0;36mThis is cyan text\033[0m");
System.out.println("\033[0;37mThis is white text\033[0m");
}
}
使用第三方库
如果你不想直接使用ANSI转义序列,可以使用一些第三方库来简化这个过程,JLine和ANSI escape code库可以帮助你轻松地输出颜色文字。
以下是一个使用JLine库的例子:

import jline.TerminalFactory;
import jline.console.ConsoleReader;
public class ColorTextWithJLine {
public static void main(String[] args) throws Exception {
ConsoleReader reader = new ConsoleReader(TerminalFactory.get());
reader.print("\033[0;31mThis is red text\033[0m");
reader.println();
reader.print("\033[0;32mThis is green text\033[0m");
reader.println();
reader.print("\033[0;33mThis is yellow text\033[0m");
reader.println();
reader.print("\033[0;34mThis is blue text\033[0m");
reader.println();
reader.print("\033[0;35mThis is purple text\033[0m");
reader.println();
reader.print("\033[0;36mThis is cyan text\033[0m");
reader.println();
reader.print("\033[0;37mThis is white text\033[0m");
reader.println();
}
}
使用Java 17的样式API
从Java 17开始,Java标准库中引入了样式API,这使得输出颜色文字变得更加简单,以下是一个使用Java 17样式API的例子:
import java.util.regex.Pattern;
public class StyleTextExample {
public static void main(String[] args) {
System.out.println("This is normal text");
System.out.println("This is " + Style.BRIGHT + "bold text" + Style.RESET);
System.out.println("This is " + Style.ITALIC + "italic text" + Style.RESET);
System.out.println("This is " + Style.UNDERLINED + "underlined text" + Style.RESET);
System.out.println("This is " + Style.BLACK + "black text" + Style.RESET);
System.out.println("This is " + Style.RED + "red text" + Style.RESET);
System.out.println("This is " + Style.GREEN + "green text" + Style.RESET);
System.out.println("This is " + Style.YELLOW + "yellow text" + Style.RESET);
System.out.println("This is " + Style.BLUE + "blue text" + Style.RESET);
System.out.println("This is " + Style.MAGENTA + "magenta text" + Style.RESET);
System.out.println("This is " + Style.CYAN + "cyan text" + Style.RESET);
System.out.println("This is " + Style.WHITE + "white text" + Style.RESET);
}
}
经验案例
假设你正在开发一个命令行工具,用于显示用户账户的状态,你可以使用ANSI转义序列来高亮显示不同的状态:
public class AccountStatus {
public static void main(String[] args) {
String accountStatus = "Active";
if ("Active".equals(accountStatus)) {
System.out.println("\033[0;32m" + accountStatus + "\033[0m");
} else if ("Inactive".equals(accountStatus)) {
System.out.println("\033[0;31m" + accountStatus + "\033[0m");
} else {
System.out.println("\033[0;33m" + accountStatus + "\033[0m");
}
}
}
FAQs
Q1: 为什么在某些终端中颜色不显示? A1: 这可能是因为你的终端不支持ANSI转义序列,或者你的Java环境没有正确配置,确保你的终端和Java环境都支持ANSI颜色代码。

Q2: 如何在Windows命令提示符中输出颜色文字?
A2: 在Windows命令提示符中,你可以使用color命令来设置背景和文字颜色。color 0A将设置背景为黑色,文字为绿色。
国内文献权威来源
《Java编程思想》(第4版),作者:埃克尔(Eckel),出版社:机械工业出版社。 《Java核心技术》(第10版),作者:霍斯特曼(Horstmann),出版社:电子工业出版社。