java,以粗体打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29109678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java, print in bold
提问by Na Dia
How can I print output in bold in printf? "[1m testing bold"
does not do anything.
如何在 printf 中以粗体打印输出?"[1m testing bold"
什么都不做。
String format = "%-20s %-15s %-20s %-15s %-10s";
System.out.printf(format, "Name", "Group_name", "Java_Prof_Level", "Cpr_Nr", "Gender", "[1m testing bold");
采纳答案by Hoopje
You cannot print bold with Java System.out
. It just streams to the standard output stream so, in principle, it is unformatted text only.
你不能用 Java 打印粗体System.out
。它只是流到标准输出流,所以原则上,它只是无格式的文本。
However, some software packages interpret special character sequences (the so-called ANSI escape sequences) to allow formatting.
但是,某些软件包会解释特殊字符序列(所谓的 ANSI 转义序列)以允许进行格式化。
Note that ANSI escape sequences start with an escape character, so you need to add that to your string also. (Try "\u001B[1m I am bold".)
请注意,ANSI 转义序列以转义字符开头,因此您还需要将其添加到字符串中。(试试"\u001B[1m 我很大胆"。)
Most Unix terminals interpret ANSI escape sequences by default. In old DOS times, you needed to use ANSI.SYSfor the escape sequences to work.
大多数 Unix 终端默认解释 ANSI 转义序列。在旧的 DOS 时代,您需要使用ANSI.SYS才能使转义序列起作用。
In Windows and the Eclipse terminal the codes do not work.
在 Windows 和 Eclipse 终端中,代码不起作用。
回答by hrdasadia
This really depends on what kind of console is being used. For IDEs like Netbeans and Eclipse, I'm not sure if you can affect the font. But for most terminals, the following escape character works:
这实际上取决于所使用的控制台类型。对于像 Netbeans 和 Eclipse 这样的 IDE,我不确定您是否可以影响字体。但对于大多数终端,以下转义字符有效:
String textInBold = "Java_Prof_Level";
System.out.print("3[0;1m" + textInBold);