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

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

问题描述

我用Java编写了一个程序,它使用了一种特殊的字体,在任何操作系统上都不存在。

有没有可能在Java中添加这个特殊的字体到操作系统?例如,在Windows中,将这种字体复制到特殊的字体文件夹。



如果可能的话,怎么样?

解决方案

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

b
$ b

Oracle Java SE 6:java.awt.Font



有一个来自Oracle的教程,显示了这个例子: / p>

  try {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT,new File(A.ttf)));
} catch(IOException | FontFormatException e){
//处理异常
}

我可能会把它包装在某种资源加载器中,但是不要每次使用它时从包中重新加载文件。



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


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

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.

If it is possible, how?

解决方案

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

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天全站免登陆