【问题标题】:What is the difference between a built-in font and a third-party font to Java?Java的内置字体和第三方字体有什么区别?
【发布时间】:2012-05-28 09:40:45
【问题描述】:
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖SSCCE。热链接到网络上的字体。
标签:
java
macos
swing
fonts
jlabel
【解决方案1】:
在带有 OpenJDK 7 update 19 的 Linux 上,它工作得很好。你还没有提供你的代码,所以我们很难看出这是否有什么问题。
当我修改示例以使用 Roboto 时,换行符按预期工作。
我不知道您使用的是哪个 Java 运行时,但如果您使用的是 Apple 提供的那个,则 FontMetrics 对象返回的值可能会有所不同(因为 Apple Java 运行时的字体呈现不同,例如Linux 上的 OpenJDK)。我建议尝试 OpenJDK 看看是否会有所不同,或者在不同的平台上运行您的应用程序,看看是否会有所不同。
Font robotoFont = null;
try {
robotoFont = Font.createFont(Font.TRUETYPE_FONT, new File("/usr/share/fonts/roboto/Roboto-Black.ttf"));
robotoFont = robotoFont.deriveFont(14f);
} catch (Exception e) {
e.printStackTrace();
}
// Using the MultiLineLabel class.
final MultiLineLabel mLabel = new MultiLineLabel(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
+ "Phasellus non sapien quam. Fusce posuere, nisl "
+ "vitae tristique volutpat, augue erat faucibus nisl, "
+ "nec venenatis metus sem vel enim. Cras in libero "
+ "sapien, vitae euismod neque. Proin hendrerit, odio "
+ "et faucibus suscipit, eros tellus blandit justo, "
+ "ac cursus risus elit ut risus.");
mLabel.setForeground(Color.WHITE);
mLabel.setFont(robotoFont);