Java:尝试使用自定义字体已关闭流
我正在尝试用java创建一个自定义字体,我尝试了很多不同的方法,但当我导出jar时,它就是不起作用。这是我目前的代码:Java:尝试使用自定义字体已关闭流,java,swing,fonts,jlabel,custom-font,Java,Swing,Fonts,Jlabel,Custom Font,我正在尝试用java创建一个自定义字体,我尝试了很多不同的方法,但当我导出jar时,它就是不起作用。这是我目前的代码: Font font = null; try { font = Font.createFont(0, new File("res/fonts/PlayTheGame.ttf")); font.deriveFont(20f); } catch (FontFormatException | IOException e) { e.printStackTrace(
Font font = null;
try {
font = Font.createFont(0, new File("res/fonts/PlayTheGame.ttf"));
font.deriveFont(20f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
Font fntPlayTheGame = font.deriveFont(0, 40f);
lblTitle = new JLabel("Cell Defender");
lblTitle.setFont(fntPlayTheGame);
该字体在eclipse内部工作,但当我在编译后运行它时,它会说它无法读取该字体。我试过使用InputStream
和其他东西,但就是不起作用
我如何才能让它工作?假设字体是一个内部资源,你应该使用
getClass().getResourceAsInputStream(“res/fonts/PlayTheGame.ttf”)
而不是只给我java.io.IOException:字体数据读取问题。
要更快地获得更好的帮助,请发布一条消息。我知道了,当您使用路径作为/font/.ttf
而不是在第一个斜杠前使用res
时,它会有所帮助。请将此问题全部删除,而不是