页面显示文字的java代码,在命令行java中突出显示文本

I have an assignment to recreate the unix cal program, fairly straightforward except for one part. On the current day, it highlights the number. I have no idea how to do this. Any idea on how to do it in Java?

Image:

KZ8FTpd.png

解决方案

ANSI color codes

The color of the prompt is set by expanding the escape sequence

“\e[sm”, where s is a semicolon-delimited list of ANSI color codes:

“\e[31;44;1m” would set the foreground color to red, the background to

blue, and the font in bold face; (The “\e” is the ASCII Escape

character. Don’t forget to terminate the sequence with the “m”

character.)

Binary sequences in the environment variables need to be set off by

indicators that they have zero width, or else the shell won’t

calculate correctly the width of the prompt. Bash encases such things

with slash-brackets “[ .. ]”, whereas Tcsh uses percent-brace “%{ ..

%}”.

The codes:

0 restore default color

1 brighter

2 dimmer

4 underlined text

5 flashing text

7 reverse video

black red green yellow blue purple cyan white

foreground 30 31 32 33 34 35 36 37

background 40 41 42 43 44 45 46 47

So in order to do this through Java, you need to set

System.out.println(characterCode + character);

where String characterCode = "\033[31;44;1m"; and char character = 'A';

and you would get an A with foreground color set to red, the background to blue, and the font in bold...

EDIT : Results of a test in Xubuntu

public static void main(String[] args) {

char character = 'A';

String characterCode;

for (int foreground = 30; foreground < 38; foreground++) {

for (int background = 40; background < 48; background++) {

characterCode = "\033[" + foreground + ";" + background + ";1m";

System.out.print(characterCode + character);

}

System.out.println();

}

}

nuvRV.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值