[英]Jar file not using custom font
Inside NetBeans my custom font loads properly from this set of code but fails to load when I run my program from the executable jar file 在NetBeans内部,我的自定义字体可以从这组代码正确加载,但是当我从可执行jar文件运行程序时无法加载
code 码
public static void main(String[] args) {
Arcanus arc = new Arcanus();
try {
Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("Golden-Sun.ttf")).deriveFont(12f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("Golden-Sun.ttf")));
arc.setFont(customFont);
} catch (IOException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
}
}
any help would be apreciated 任何帮助将不胜感激
Embedded resources should NOT be read from a File
object. 嵌入的资源不应该从一个读取File
的对象。 The File
object is used for reading files in the local file system. File
对象用于读取本地文件系统中的文件。 Once your file is jarred, it becomes a resource and should be read as such. 一旦您的文件被震撼,它就成为资源,应该这样阅读。 You can read it as a InputStream
by using getClass().getResourceAsStream()
. 您可以使用getClass().getResourceAsStream()
其作为InputStream
读取。 For example 例如
InputStream is = getClass().getResourceAsStream("/Golden-sun.tff");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
Where Golden-sun.tff
is on the class path (direct child of src
for development) Golden-sun.tff
在类路径上的位置(用于开发的src
直接子级)
Root
src
Golden-sun.tff
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.