[英]Add custom font file on java for executable JAR file
我正在尝试将自定义字体添加到我的 jswing 应用程序中,但我找不到如何正确上传 .ttf 文件,以便在创建可执行 jar 文件后可以使用它。
问题在这里, new File(getClass().getResourceAsStream("/"+"cs_regular.ttf"))
谢谢你!
try {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(getClass().getResourceAsStream("/"+"cs_regular.ttf"))));
} catch (FontFormatException|IOException e) {
pr("Error_107");
}
It is not quite easy to create java.io.File
from stream, you can create Font from stream directly. 看看这个答案。 https://stackoverflow.com/a/40862139/10999348
这是我现在的工作代码
InputStream stream =ClassLoader.getSystemClassLoader().getResourceAsStream("cs_regular.ttf");
try {
csfont = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(48f);
} catch (FontFormatException|IOException e) {
//handle exeption
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(csfont);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.