Java 中的默认字体
我有一些 Swing 代码(在 1.6 中为 1.6 编写),专门将文本区域中的字体设置为 Courier,但在某些 Windows 系统上,这显示为 Arial(系统默认值?)。 这是否意味着系统中缺少字体? 当 Java 找不到它要查找的字体时,它会采取什么行为? 它抱怨吗? 它是否将其记录在某处? 它是否立即诉诸使用系统默认值? 1.4/1.5/1.6 版本的 JVM 之间的行为是否不同?
还有其他人遇到过这个吗? 我非常惊讶地发现 UI 中显示的内容与我硬编码到应用程序中的内容不同 - 并且仅在某些系统上。 核心问题是我需要针对这种特殊情况使用等宽字体样式,而 Arial 不是等宽字体。 如果找不到某种字体,是否有某种方法可以指定后备? 例如:
如果字体可用,请使用“Courier” 否则使用“等宽”
???
I have some Swing code (written in 1.6 for 1.6) that specifically sets the font in a text area to Courier, but on some Windows systems, this shows up as Arial (the system default?) instead. Does that mean a font is missing from the system? What is the behavior Java has when it can't find a font it is looking for? Does it complain? Does it log it somewhere? Does it immediately resort to using the system default? Is the behavior different between 1.4/1.5/1.6 versions of the JVM?
Has anyone else ever run into this? I was very surprised to have something different from what I HARDCODED into the application show up in the UI - and only on some systems. The core issue is that I need a monospaced font style for this particular case, and Arial is not monospaced. Is there some way to specify a fallback if a certain font is not found? Something like:
if font is available use "Courier"
else use "monospaced"
???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该首先检查“Courier”是否在
GraphicsEnvironment.getAvailableFontFamilyNames()
的结果中我不知道 Java 中有任何内置机制用于“
if-unavailable-fallback-to< /代码>”行为。
You should first check if "Courier" is among the results of
GraphicsEnvironment.getAvailableFontFamilyNames()
I don't know any built-in mechanism in Java for "
if-unavailable-fallback-to
" behavior.根据这篇文章1 Microsoft 提供的唯一等宽 TrueType 字体是 Courier New 和 Lucida Sans Typewriter。
According to this article1 the only monospaced TrueType fonts shipped by Microsoft are Courier New and Lucida Sans Typewriter.
它可能希望您为 Courier 指定“Courier New”。 我没有Windows系统,所以无法验证这一点。
It might expect you to specify "Courier New" for Courier. I don't have a Windows system, so I can't verify this.