我想知道在Java中是否有一种方法可以设置控制台输出文本的颜色。该程序将仅在我的Windows 7 x64笔记本电脑上运行,因此它是否与系统相关并不重要。
几周前有人提出了这个问题:Change color in java eclipse console ,并且有一个很好的解决方案(由@VonC)来解决类似的问题,但它只解决了Eclipse内部的问题。
如果我从命令行执行程序,能否实现相同的效果? 如果可以,怎么做?
我想知道在Java中是否有一种方法可以设置控制台输出文本的颜色。该程序将仅在我的Windows 7 x64笔记本电脑上运行,因此它是否与系统相关并不重要。
几周前有人提出了这个问题:Change color in java eclipse console ,并且有一个很好的解决方案(由@VonC)来解决类似的问题,但它只解决了Eclipse内部的问题。
如果我从命令行执行程序,能否实现相同的效果? 如果可以,怎么做?
试试这个.... 此外,阅读http://jansi.fusesource.org/
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";
你可能会对另一个库Jansi感兴趣:http://jansi.fusesource.org/
Jansi解释了ANSI代码并将其格式化为控制台输出。它适用于Unix和Windows。
更新11/2014:你还可以查看Github页面
虽然与Java控制台输出无直接关系,但如果您想在Kotlin控制台输出中使用ANSI颜色,则可以使用这个很棒的库 - https://github.com/importre/crayon。
package util;
public class Colors {
public static final String reset = "\u001B[0m";
public static final String bold = "\u001b[1m";
public static final String italic = "\u001b[3m";
public static final String underline = "\u001b[4m";
public static final String reversed = "\u001b[7m";
public static final String black = "\u001b[30m";
public static final String blue = "\u001b[34m";
public static final String cyan = "\u001b[36m";
public static final String green = "\u001b[32m";
public static final String magenta = "\u001b[35m";
public static final String red = "\u001b[31m";
public static final String white = "\u001b[37m";
public static final String yellow = "\u001b[33m";
public static final String brightBlack = "\u001b[30;1m";
public static final String brightBlue = "\u001b[34;1m";
public static final String brightCyan = "\u001b[36;1m";
public static final String brightGreen = "\u001b[32;1m";
public static final String brightMagenta = "\u001b[35;1m";
public static final String brightRed = "\u001b[31;1m";
public static final String brightWhite = "\u001b[37;1m";
public static final String brightYellow = "\u001b[33;1m";
public static final String bgBlack = "\u001b[40m";
public static final String bgBlue = "\u001b[44m";
public static final String bgCyan = "\u001b[46m";
public static final String bgGreen = "\u001b[42m";
public static final String bgMagenta = "\u001b[45m";
public static final String bgRed = "\u001b[41m";
public static final String bgWhite = "\u001b[47m";
public static final String bgYellow = "\u001b[43m";
public static final String bgBrightBlack = "\u001b[40;1m";
public static final String bgBrightBlue = "\u001b[44;1m";
public static final String bgBrightCyan = "\u001b[46;1m";
public static final String bgBrightGreen = "\u001b[42;1m";
public static final String bgBrightMagenta = "\u001b[45;1m";
public static final String bgBrightRed = "\u001b[41;1m";
public static final String bgBrightWhite = "\u001b[47;1m";
public static final String bgBrightYellow = "\u001b[43;1m";
}