我们被要求制作一个程序,该程序将读取文本文件并显示Unicode字符的摘要。执行此操作时,我遇到一些Unicode字符的问题,这些字符似乎在我的控制台中打印为问号。但是,当我使用Swing输出相同的Unicode文本时,它不再是问号
System.out.println("\u0126"); // appears to be ? in my console.
JOptionPane.showMessageDialog(null,"\u0126"); // seems to display the character successfuly
我可以把问题抛在脑后,因为我要使用GUI,但我想要一个解释,像我这样的初学者可以理解。
为什么某些Unicode字符似乎是在控制台中打印的问号,但在Swing中没有正确打印? (Eclipse,NetBeans,JCreator,JGrasp都这样做,我认为我的IDE存在问题)。
编码或字体有问题吗?为了在控制台中成功显示Unicode文本,我该怎么做才能在将来不会出现问号?
答案 0 :(得分:2)
UNICODE生成的字符将取决于您正在使用的字体的字符gylph。大多数字体只有完整UNICODE标准的子集。例如,如果您想要显示简体中文,您使用的字体必须包含简体中文的字形。
UNICODE Consortium有一些相关信息。
答案 1 :(得分:1)
如何在Java中插入表情符号和精美的Unicode字符?
我花了一些时间自行寻找解决方案,希望对您有所帮助:)
// To run emojis in Java make sure you have installed Windows Terminal
// and in order to insert them, remember Java char only support 16-bit
// characters meanwhile a great part of Unicode is 32-bit sooooo
// every single Unicode character must be break into two 16-bit
// character, these are called surrogates.
// How to create surrogates and use emojis in Java! :D
//1. Copy Uni code you want to use (Ommit U+, that's only a label)
// https://unicode-table.com/en/1F680/
//2. Paste in here and copy both surrogates
// http://www.russellcottrell.com/greek/utilities/SurrogatePairCalculator.htm
//3. Code it as Strings and separate it by \u symbol in Java.
//4. Compile it and run it in Windows Terminal.
// #HappyCoding!!
// One small thing: For some reason you need to put two spaces after
// every two surrogates, otherwise it'll print two question marks next
// to it.
// Other small thing: Try to use emojis at the end of a line because for
// some reason if you insert String after the Unicode it'll do strange
// things.
String rocket = "Hello Entrepeneurs \uD83D\uDE80 ";
System.out.print(rocket);
答案 2 :(得分:0)
u0126 - >是大多数终端输出的ANSCII(或其所谓的)之外的字符。该角色实际上是Ħ,但这只会显示您的控制台是否可以显示该角色。
您可以看到安装utf-8是否允许您显示这样的字符。