我试图在"\uD83D"
文本上显示此Unicode JButton
,但是在编译时,它仅显示未知字符的平方。
最佳答案
Thomas给出了很好的答案,但是请注意,为了避免猜测已安装的字体支持字符或字符串,我们可以迭代可用的字体并使用canDisplayUpTo
的Font
重载方法进行检查:Font.canDisplayUpTo(String)
Font.canDisplayUpTo(CharacterIterator,start,limit)
Font.canDisplayUpTo(char[],start,limit)
例如。
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class FontCheck {
public static void main(String[] args) {
String s = "\u4E33";
Font[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAllFonts();
System.out.println("Total fonts: \t" + fonts.length);
int count = 0;
for (Font font : fonts) {
if (font.canDisplayUpTo(s) < 0) {
count++;
System.out.println(font.getName());
}
}
System.out.println("Compatible fonts: \t" + count);
}
}
输出:
Total fonts: 391
Arial Unicode MS
Dialog.bold
Dialog.bolditalic
Dialog.italic
Dialog.plain
DialogInput.bold
DialogInput.bolditalic
DialogInput.italic
DialogInput.plain
Microsoft JhengHei
Microsoft JhengHei Bold
Microsoft JhengHei Light
Microsoft JhengHei UI
Microsoft JhengHei UI Bold
Microsoft JhengHei UI Light
Microsoft YaHei
Microsoft YaHei Bold
Microsoft YaHei Light
Microsoft YaHei UI
Microsoft YaHei UI Bold
Microsoft YaHei UI Light
Monospaced.bold
Monospaced.bolditalic
Monospaced.italic
Monospaced.plain
NSimSun
SansSerif.bold
SansSerif.bolditalic
SansSerif.italic
SansSerif.plain
Serif.bold
Serif.bolditalic
Serif.italic
Serif.plain
SimSun
Compatible fonts: 35