Java控制台中的颜色 [英] Colours in Java console

查看:742
本文介绍了Java控制台中的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想知道是否有任何方式在Java控制台应用程序中指定背景和文本的颜色。我在写一个控制台菜单。

解决方案



您可以使用 ANSI转义代码指定文字的前景和背景颜色控制台应用程序在许多平台上,假设您正在使用的终端运行应用程序支持ANSI模式。你不需要任何额外的库来使用这些代码,你可以直接将它们嵌入你的字符串。但是,由于它们看起来有点乱,您可以使用 JCurses 等库



以下是一个示例程序:

 <$> c $ c> public static void main(String args []){
System.out.println((char)27 +[01; 31m;这段文本是红色的。 00; 00m);
System.out.println((char)27 +[01; 32m;此文本是绿色的+(char)27 +[00; 00m
}

作为一个好处,ANSI转义码将帮助您进行屏幕清除和光标定位,以及。


basically, I was wondering if there is any way of specifying the colours of the background and text in a Java console application. I am writing a console menu. Also, if there is any easy way of clearing the console.

Thanks.

解决方案

You can use ANSI escape codes to specify the foreground and background colors of text for console apps on many platforms, assuming the terminal you're using to run the application supports ANSI mode. You don't need any additional libraries to use these codes, you can just embed them directly in your strings. However, since they're a bit messy looking, you can use a library such as JCurses to make it a bit easier to apply the various ANSI codes.

Here's an example program:

public static void main(String args[]) {
    System.out.println((char)27+"[01;31m;This text is red."+(char)27+"[00;00m");
    System.out.println((char)27+"[01;32m;This text is green."+(char)27+"[00;00m");
}

As a bonus, ANSI escape codes will help you with screen clearing and cursors positioning, as well.

这篇关于Java控制台中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆