如何在 Java 中使用自定义字体? [英] How can I use a custom font in Java?

查看:40
本文介绍了如何在 Java 中使用自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Java 编写了一个程序,该程序使用了默认情况下任何操作系统都不存在的特殊字体.

I wrote a program in Java that uses a special font that by default doesn't exist on any operating system.

在 Java 中是否可以将这种特殊字体添加到操作系统中?例如,在 Windows 中,将此字体复制到特殊的 Fonts 文件夹中.

Is it possible in Java to add this special font to the operation system? For example, in Windows, to copy this font to the special Fonts folder.

如果可以,怎么做?

推荐答案

如果您的包中包含字体文件(otf、ttf 等),您可以通过此处描述的方法在应用程序中使用该字体:

If you include a font file (otf, ttf, etc.) in your package, you can use the font in your application via the method described here:

Oracle Java SE 6:java.awt.Font

Oracle 提供的教程显示了这一点例子:

There is a tutorial available from Oracle that shows this example:

try {
     GraphicsEnvironment ge = 
         GraphicsEnvironment.getLocalGraphicsEnvironment();
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));
} catch (IOException|FontFormatException e) {
     //Handle exception
}

我可能会将其封装在某种资源加载器中,以免每次要使用它时都从包中重新加载文件.

I would probably wrap this up in some sort of resource loader though as to not reload the file from the package every time you want to use it.

与您的原始问题更密切相关的答案是将字体安装为应用程序安装过程的一部分.该过程将取决于您选择的安装方法.如果它不是桌面应用程序,则必须查看提供的链接.

An answer more closely related to your original question would be to install the font as part of your application's installation process. That process will depend on the installation method you choose. If it's not a desktop app you'll have to look into the links provided.

这篇关于如何在 Java 中使用自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆