Jar文件不使用自定义字体

时间:2014-06-27 19:54:25

标签: java true-type-fonts resource-loading

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

任何帮助都是apreciated

1 个答案:

答案 0 :(得分:2)

嵌入式资源应 NOT File对象中读取。 File对象用于读取本地文件系统中的文件。一旦你的文件受到震动,它就会成为一种资源,应该这样阅读。您可以使用InputStream将其作为getClass().getResourceAsStream()阅读。例如

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

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

Root
   src
      Golden-sun.tff