从Jar访问自定义字体 [英] Accessing custom font from Jar

查看:195
本文介绍了从Jar访问自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在我的应用程序中使用自定义字体。为此,我使用 Font.createFont()方法。
我的代码如下。当我使用命令运行我的主类时它工作正常

I am going to use custom font in my application. For that I am using Font.createFont() method. My code is given below. It works fine when I run my main class using command


java myAppl.class

java myAppl.class

字体文件与我的类文件位于同一目录中。但是当我将所有文件和字体文件捆绑在JAR中然后从JAR运行我的应用程序时,不会加载自定义字体。为什么?

The font file is in same directory that of my class file. But when I bundled all files and font files in JAR and then run my application from JAR, the custom font do not loaded. Why?

InputStream is = this.getClass().getResourceAsStream("myfont.TTF");
uniFont=Font.createFont(Font.TRUETYPE_FONT,is);
Font f = uniFont.deriveFont(24f);

我该怎么办?

推荐答案


  1. 确保字体文件名称&扩展在代码中与文件系统完全相同。 Windows可能不区分大小写,但Java是。

  2. 检查 getResourceAsStream()返回的 InputStream / code> for null 。如果它是 null ,则表示资源未找到。

  3. 将字体放在Jar的根目录中并添加/ 作为名称的前缀。

  1. Make sure the case of the font file name & extension is exactly the same in code as on the file system. Windows may not be case sensitive, but Java is.
  2. Check the InputStream returned by getResourceAsStream() for null. If it is null, that indicates the resources was not located.
  3. Put the font in the root of the Jar and add "/" as the prefix to the name.

这篇关于从Jar访问自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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