更改java控制台输出的颜色

发布于 2024-11-05 20:11:32 字数 329 浏览 1 评论 0 原文

我想知道是否有什么办法可以设置我在 Java 中输出到控制台的文本的颜色。是否特定于系统并不重要,因为该程序只能在我的 Windows 7 x64 笔记本电脑上运行。

这个问题: Change color in java eclipse console 几周前被问及一个很好的解决方案(由@VonC)解决类似的问题,但它只解决了eclipse内部的问题。

如果我从命令行执行我的程序可以达到同样的效果吗?如果是这样怎么办?

I was wondering if there is someway for me to set the color of the text that I output to the console in Java. It does not matter if it is system specific as the program will only be run on my Windows 7 x64 laptop.

This question: Change color in java eclipse console was asked several weeks ago and had a good solution(by @VonC) to a similar problem however it only addressed the issue inside eclipse.

Can the same effect be achieved if I execute my program from the command line? and if so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

や莫失莫忘 2024-11-12 20:11:32

你这个……
进一步阅读 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";

thy this....
furthermore read 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";
与他有关 2024-11-12 20:11:32

您可能感兴趣的另一个库是 Jansi: http://jansi.fusesource.org/

Jansi 解释 ANSI编码并格式化它们以用于控制台输出。它适用于 UNIX 和 Windows。

更新 11/2014: 您还可以查看 github 页面

Another library you may be interested in is Jansi: http://jansi.fusesource.org/

Jansi interprets ANSI code and format them for the console output. It works for both unix and windows.

Update 11/2014: you can also see the github Page

北笙凉宸 2024-11-12 20:11:32

与 Java 控制台输出没有直接关系,但如果您希望在 Kotlin 控制台输出中使用 ANSI 颜色,这是一个很棒的库 - https://github.com/importre/crayon

Not directly related to Java console output, but if you're looking to use ANSI colors in Kotlin console output, this is a great library to use - https://github.com/importre/crayon

奢望 2024-11-12 20:11:32

我的项目需要颜色。这里为大家准备了一堂课。只需执行 Colors.(color) +“whatever”即可添加颜色、粗体或斜体。使用 Colors.reset 重置颜色。希望这有帮助。

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

I needed colors for a project. Here is a class for everyone. Just do Colors.(color) + "whatever" to add color, bold, or italic. Use Colors.reset to reset the colors. Hope this helps.

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文