gpt4 book ai didi

java - Jar 文件不使用自定义字体

转载 作者:行者123 更新时间:2023-12-02 05:41:36 25 4
gpt4 key购买 nike

在 NetBeans 内部,我的自定义字体可以从这组代码正确加载,但当我从可执行 jar 文件运行程序时无法加载

代码

    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();
}
}

如有任何帮助,我们将不胜感激

最佳答案

嵌入资源不应该从File 对象中读取。 File 对象用于读取本地文件系统中的文件。一旦你的文件被打包,它就变成了一种资源,并且应该被这样读取。您可以使用 getClass().getResourceAsStream() 将其作为 InputStream 读取。例如

InputStream is = getClass().getResourceAsStream("/Golden-sun.tff");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);

其中 Golden-sun.tff 位于类路径上(用于开发的 src 的直接子级)

Root
src
Golden-sun.tff

关于java - Jar 文件不使用自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com