2011-04-23 6 views

回答

363

如果您的終端支持它,您可以使用ANSI escape codes在輸出中使用顏色。它通常適用於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); 
+0

@ WhiteFang34你可以解釋一下,如果它的顏色是黑色,至少在我的控制檯中,RESET的用途是什麼?這是否像一個默認或某事? – Boro 2011-04-23 08:31:25

+1

@Boro:重置代碼會關閉迄今爲止設置的所有ANSI屬性,這應該將控制檯恢復爲默認值。如果你不知道默認顏色,或者也使用其他一些屬性,如背景顏色,字體樣式等,這很有用。 – WhiteFang34 2011-04-23 08:38:10

+1

jansi真的很棒!對於那些在Eclipse中開發的人,我可以推薦這個插件:http://mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console/和一段代碼來啓用顏色,如果代碼是不是在控制檯中執行:'if(System.console()== null)System.setProperty(「jansi.passthrough」,「true」);' – 2014-04-13 19:51:16

13

一個相當便攜的方式是使用原始轉義序列。見http://en.wikipedia.org/wiki/ANSI_escape_code

[編輯的user9999999於2017年2月20日]

Java不「處理代碼」,這是真的,但Java的輸出你告訴它來輸出。 Windows控制檯將ESC(chr(27))視爲另一個字形(←),這不是Java的錯。

you made me boot into Windows. you owe me, bro

+0

這不起作用,因爲Java IO層不會將它們轉換爲顏色。 System.out.println((char)27 +「[31; 1mERROR」+(char)27 +「[0m」只產生「[31; 1mERROR [0m」從Windows cmd.com作爲可執行文件運行時.jar – simpleuser 2017-02-12 06:03:33

+0

這個問題沒有被標記爲'windows'.Windows控制檯從來沒有符合ANSI的標準,我記得 – 2017-02-12 14:52:50

+0

但問題是,java不處理代碼,無論cmd.com的支持 – simpleuser 2017-02-20 17:31:44

30

我創建了一個名爲JCDP庫(的Java調試彩色打印機)。

對於Linux,它使用WhiteFang提到的ANSI轉義碼,但使用而不是代碼對其進行了摘要,這更直觀。

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

對於Windows它實際上包括JAnsi庫,但在它創建了一個抽象層,保持對Linux產生的直觀和簡單的界面:當它變得容易。

此庫許可證號爲MIT License,因此請隨時使用它。

看一看JCDP's github repository

+0

還有一個錯字,其中一個子彈拼寫動態拼寫。 – 2012-01-14 18:37:30

+0

@DiAlex如果我能得到你的API副本,我很樂意。主頁上的鏈接不再起作用,而是導致「404」。我可以在哪裏下載? – 2015-01-05 06:09:50

+0

解決了,謝謝你讓我知道 – dialex 2017-12-29 09:17:42

5

您可以使用ANSI轉義序列來執行此操作。實際上,我已經將這個類放在了Java中,供任何想要簡單解決此問題的人使用。它允許在文本中使用自定義顏色代碼。

https://gist.github.com/nathan-fiscaletti/9dc252d30b51df7d710a

實施例使用:

WITH背景顏色的顏色代碼格式 - >:前景,背景:

顏色代碼格式沒有背景顏色 - >:前景,N :

重置顏色格式 - > [RC]

String ansiColoredString = ColorCodes.parseColors("Hello, This :blue,n:is[RC] a :red,white:response[RC]."); 

String ansiColoredString = ColorCodes.RED + "Hello" + ColorCodes.WHITE + ", This is a " + ColorCodes.BLUE + "test"; 
14

下面是在Java類的顏色與public static字段

用法

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

列表0 注意 如果有人正在尋找一個快速解決方案,不要忘記使用RESET作爲影響印刷後仍將如果它不被清除


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 

    // Background 
    public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK 
    public static final String RED_BACKGROUND = "\033[41m"; // RED 
    public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN 
    public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW 
    public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE 
    public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE 
    public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN 
    public static final String WHITE_BACKGROUND = "\033[47m"; // 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 
} 
3

,隨意使用下面的輔助類:)

public class Log { 

    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"; 

    //info 
    public static void i(String className, String message) { 
     System.out.println(ANSI_GREEN + className + " : " + message + ANSI_RESET); 
    } 

    //error 
    public static void e(String className, String message) { 
     System.out.println(ANSI_RED + className + " : " + message + ANSI_RESET); 
    } 

    //debug 
    public static void d(String className, String message) { 
     System.out.println(ANSI_BLUE + className + " : " + message + ANSI_RESET); 
    } 

    //warning 
    public static void w(String className, String message) { 
     System.out.println(ANSI_YELLOW + className + " : " + message + ANSI_RESET); 
    } 

} 

用法:

Log.i(TAG,"This is an info message"); 

Log.e(TAG,"This is an error message"); 

Log.w(TAG,"This is a warning message"); 

Log.d(TAG,"This is a debug message"); 

Thanks @ whiteFang34 for the ANSI codes。

相關問題