如何使用System.out.println在控制台中打印颜色?

如何在控制台中打印颜色? 我想在处理器发送数据时用颜色显示数据,并在接收数据时用不同的颜色显示数据。


如果您的终端支持它,则可以使用ANSI转义码在输出中使用颜色。 它通常适用于Unix shell提示; 但是,它不适用于Windows命令提示符(尽管它对Cygwin有效)。 例如,你可以为这些颜色定义这些常量:

public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";

然后,您可以根据需要参考这些内容。

例如,使用上述常量,可以在受支持的终端上输出以下红色文本:

System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);

更新:您可能想要查看Jansi图书馆。 它提供了一个API,并使用JNI支持Windows。 我还没有尝试过; 不过,它看起来很有希望。

更新2:另外,如果您希望将文本的背景颜色更改为不同的颜色,则还可以尝试以下操作:

public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";

例如:

System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);

我创建了一个名为JCDP(Java Colored Debug Printer)的库。

对于Linux,它使用WhiteFang提到的ANSI转义代码,但是使用单词而不是直接更直观的代码来抽象它们。 它变得和以下一样容易:

print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);

对于Windows,它实际上包含JAnsi库,但在其上创建一个抽象层,维护为Linux创建的直观和简单的界面。

该图书馆根据MIT许可证获得许可,因此可以随意使用它。

看看JCDP的github存储库。


以下是带有public static字段的Java类中的颜色列表

用法

System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");


注意不要忘记在打印后使用RESET ,因为如果未清除,效果将保留


public class ConsoleColors {
    // Reset
    public static final String RESET = "33[0m";  // Text Reset

    // Regular Colors
    public static final String BLACK = "33[0;30m";   // BLACK
    public static final String RED = "33[0;31m";     // RED
    public static final String GREEN = "33[0;32m";   // GREEN
    public static final String YELLOW = "33[0;33m";  // YELLOW
    public static final String BLUE = "33[0;34m";    // BLUE
    public static final String PURPLE = "33[0;35m";  // PURPLE
    public static final String CYAN = "33[0;36m";    // CYAN
    public static final String WHITE = "33[0;37m";   // WHITE

    // Bold
    public static final String BLACK_BOLD = "33[1;30m";  // BLACK
    public static final String RED_BOLD = "33[1;31m";    // RED
    public static final String GREEN_BOLD = "33[1;32m";  // GREEN
    public static final String YELLOW_BOLD = "33[1;33m"; // YELLOW
    public static final String BLUE_BOLD = "33[1;34m";   // BLUE
    public static final String PURPLE_BOLD = "33[1;35m"; // PURPLE
    public static final String CYAN_BOLD = "33[1;36m";   // CYAN
    public static final String WHITE_BOLD = "33[1;37m";  // WHITE

    // Underline
    public static final String BLACK_UNDERLINED = "33[4;30m";  // BLACK
    public static final String RED_UNDERLINED = "33[4;31m";    // RED
    public static final String GREEN_UNDERLINED = "33[4;32m";  // GREEN
    public static final String YELLOW_UNDERLINED = "33[4;33m"; // YELLOW
    public static final String BLUE_UNDERLINED = "33[4;34m";   // BLUE
    public static final String PURPLE_UNDERLINED = "33[4;35m"; // PURPLE
    public static final String CYAN_UNDERLINED = "33[4;36m";   // CYAN
    public static final String WHITE_UNDERLINED = "33[4;37m";  // WHITE

    // Background
    public static final String BLACK_BACKGROUND = "33[40m";  // BLACK
    public static final String RED_BACKGROUND = "33[41m";    // RED
    public static final String GREEN_BACKGROUND = "33[42m";  // GREEN
    public static final String YELLOW_BACKGROUND = "33[43m"; // YELLOW
    public static final String BLUE_BACKGROUND = "33[44m";   // BLUE
    public static final String PURPLE_BACKGROUND = "33[45m"; // PURPLE
    public static final String CYAN_BACKGROUND = "33[46m";   // CYAN
    public static final String WHITE_BACKGROUND = "33[47m";  // WHITE

    // High Intensity
    public static final String BLACK_BRIGHT = "33[0;90m";  // BLACK
    public static final String RED_BRIGHT = "33[0;91m";    // RED
    public static final String GREEN_BRIGHT = "33[0;92m";  // GREEN
    public static final String YELLOW_BRIGHT = "33[0;93m"; // YELLOW
    public static final String BLUE_BRIGHT = "33[0;94m";   // BLUE
    public static final String PURPLE_BRIGHT = "33[0;95m"; // PURPLE
    public static final String CYAN_BRIGHT = "33[0;96m";   // CYAN
    public static final String WHITE_BRIGHT = "33[0;97m";  // WHITE

    // Bold High Intensity
    public static final String BLACK_BOLD_BRIGHT = "33[1;90m"; // BLACK
    public static final String RED_BOLD_BRIGHT = "33[1;91m";   // RED
    public static final String GREEN_BOLD_BRIGHT = "33[1;92m"; // GREEN
    public static final String YELLOW_BOLD_BRIGHT = "33[1;93m";// YELLOW
    public static final String BLUE_BOLD_BRIGHT = "33[1;94m";  // BLUE
    public static final String PURPLE_BOLD_BRIGHT = "33[1;95m";// PURPLE
    public static final String CYAN_BOLD_BRIGHT = "33[1;96m";  // CYAN
    public static final String WHITE_BOLD_BRIGHT = "33[1;97m"; // WHITE

    // High Intensity backgrounds
    public static final String BLACK_BACKGROUND_BRIGHT = "33[0;100m";// BLACK
    public static final String RED_BACKGROUND_BRIGHT = "33[0;101m";// RED
    public static final String GREEN_BACKGROUND_BRIGHT = "33[0;102m";// GREEN
    public static final String YELLOW_BACKGROUND_BRIGHT = "33[0;103m";// YELLOW
    public static final String BLUE_BACKGROUND_BRIGHT = "33[0;104m";// BLUE
    public static final String PURPLE_BACKGROUND_BRIGHT = "33[0;105m"; // PURPLE
    public static final String CYAN_BACKGROUND_BRIGHT = "33[0;106m";  // CYAN
    public static final String WHITE_BACKGROUND_BRIGHT = "33[0;107m";   // WHITE
}
链接地址: http://www.djcxy.com/p/15315.html

上一篇: How to print color in console using System.out.println?

下一篇: Programmatically Lighten or Darken a hex color (or rgb, and blend colors)