21xrx.com
2024-12-27 09:16:24 Friday
登录
文章检索 我的文章 写文章
我想跟大家分享一下如何在Java中设置字体颜色代码
2023-06-11 03:48:26 深夜i     --     --
Java 字体颜色 ANSI转义序列 控制台输出

我想跟大家分享一下如何在Java中设置字体颜色代码。在Java中,我们可以使用ANSI转义序列来设置控制台上文字的颜色。ANSI转义序列是指以ESC字符(ASCII码为27)开头,后面跟着一些特定字符组成的序列。这些特定的字符用来控制控制台输出的颜色、光标位置等。

下面是一个使用ANSI转义序列设置控制台文字颜色的Java代码示例:


public class ConsoleColors {

  // Reset

  public static final String RESET = "\033[0m"; // Text Reset

  // Regular Colors

  public static final String BLACK = "\033[0;30m";  // BLACK

  public static final String RED = "\033[0;31m";   // RED

  public static final String GREEN = "\033[0;32m";  // GREEN

  public static final String YELLOW = "\033[0;33m"; // YELLOW

  public static final String BLUE = "\033[0;34m";  // BLUE

  public static final String PURPLE = "\033[0;35m"; // PURPLE

  public static final String CYAN = "\033[0;36m";  // CYAN

  public static final String WHITE = "\033[0;37m";  // WHITE

  // Bold

  public static final String BLACK_BOLD = "\033[1;30m"; // BLACK

  public static final String RED_BOLD = "\033[1;31m";  // RED

  public static final String GREEN_BOLD = "\033[1;32m"; // GREEN

  public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW

  public static final String BLUE_BOLD = "\033[1;34m";  // BLUE

  public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE

  public static final String CYAN_BOLD = "\033[1;36m";  // CYAN

  public static final String WHITE_BOLD = "\033[1;37m"; // WHITE

  // Underline

  public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK

  public static final String RED_UNDERLINED = "\033[4;31m";  // RED

  public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN

  public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW

  public static final String BLUE_UNDERLINED = "\033[4;34m";  // BLUE

  public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE

  public static final String CYAN_UNDERLINED = "\033[4;36m";  // CYAN

  public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE

  // High Intensity

  public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK

  public static final String RED_BRIGHT = "\033[0;91m";  // RED

  public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN

  public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW

  public static final String BLUE_BRIGHT = "\033[0;94m";  // BLUE

  public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE

  public static final String CYAN_BRIGHT = "\033[0;96m";  // CYAN

  public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE

  // Bold High Intensity

  public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK

  public static final String RED_BOLD_BRIGHT = "\033[1;91m";  // RED

  public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN

  public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW

  public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE

  public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE

  public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN

  public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE

  // High Intensity backgrounds

  public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK

  public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED

  public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN

  public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW

  public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE

  public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE

  public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN

  public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m";  // WHITE

  public static void main(String[] args) {

    // Output examples

    System.out.println(RED + "This text is red!" + RESET);

    System.out.println(GREEN_BACKGROUND_BRIGHT + "This text has a green background!" + RESET);

    System.out.println(BLUE_BOLD + "This text is bold and blue!" + RESET);

  }

}

在上面的代码中,我定义了一个ConsoleColors类,并在其中定义了一些常量,用来在控制台上设置不同颜色的文本。然后在main()方法中,我通过使用这些常量,在控制台上输出了一些带有不同颜色的文本。其中,RESET用来重置控制台输出的样式。

通过使用这些ANSI转义序列,我们就可以在Java中设置控制台输出的文字颜色了。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复