如何使用 Java 查找计算机上是否正确安装了某种字体
我有一台运行Win Vista的笔记本电脑,刚买的时候,某些中文字体无法显示,只能看到矩形,但我玩了一段时间的控制设置,更改了一些属性,现在可以显示中文了字体正确,但我不记得我做了什么。
现在我的一些程序同时显示英文和中文,就像这样:“Enter | 输入”(这里的中文也意味着回车),但是如果用户没有在他的机器上正确安装中文字体,他会看到类似这样的内容this:“Enter | [][]”,我的问题是:在Java中如何检测这些字符是否会在某台机器上正确显示,如果没有,则显示“Enter”,如果是,则显示“Enter | Enter” ”。
坦率
I have a PC notebook running Win Vista, when I first bought it, certain Chinese fonts won't show up, I could only see rectangles, but I played with the control setting for a while, changed some properties, and now it shows Chinese fonts correctly, but I don't remember what I did.
Now some of my programs displays both English and Chinese, something like this : "Enter | 输入" (The Chinese here also means enter), but if a user doesn't have Chinese fonts installed properly on his machine, he will see something like this : "Enter | [][]", my question is : in Java how to detect if those characters will show up correctly on a certain machine, if not, just display "Enter", if it is, show "Enter | 输入".
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
java.awt.GraphicsEnvironment.getAvailableFontFamilyNames()
可以为您提供当前系统上安装的可用字体的列表。您还可以使用 java.awt.GraphicsEnvironment.getAllFonts() 来获取 java.awt.Font 对象。然后,您可以使用 java.awt.Font.canDisplay(int) 来检查 Unicode 字符是否可以以该字体显示(其中 int 是整数表示多字节字符)。
java.awt.GraphicsEnvironment.getAvailableFontFamilyNames()
can give you a list of the available fonts installed on the current system. You could also usejava.awt.GraphicsEnvironment.getAllFonts()
to getjava.awt.Font
objects.Then, you can use
java.awt.Font.canDisplay(int)
to check whether a Unicode character can be displayed in that font (where theint
is the integer representation of the multibyte character).懒人版:
Lazy version:
对于那些仍然感兴趣的人。性能提示:使用
getAvailableFontFamilyNames(Locale.ROOT)
可能比仅getAvailableFontFamilyNames()
快得多,因为在后一种情况下会执行区域设置感知处理。For those who are still interested. Performance tip: using
getAvailableFontFamilyNames(Locale.ROOT)
might be significantly faster than justgetAvailableFontFamilyNames()
because in the latter case locale-aware processing is performed.