将 .ttf 文件添加到 java 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16281291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add .ttf file to java project
提问by newbee
I have downloaded akshar.ttf file and want to add it to my java project. I have tried the following ways by searching online but nothing worked so far.
我已经下载了 akshar.ttf 文件并想将它添加到我的 java 项目中。我通过在线搜索尝试了以下方法,但到目前为止没有任何效果。
Try 1:
尝试 1:
Font ttfBase = null;
Font ttfReal = null;
try {
InputStream myStream = new BufferedInputStream(new FileInputStream("akshar.TTF"));
ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("akshar font not loaded.");
}
Try 2:
尝试2:
Font font = new Font("akshar",Font.PLAIN,15);
I have the akshar.ttf file at the following places:-
我在以下位置有 akshar.ttf 文件:-
- java/jre/lib/fonts
- bin folder of my project
- src folder of my project
- java/jre/lib/字体
- 我的项目的 bin 文件夹
- 我的项目的 src 文件夹
I am new to java and have tried all these by following various links online. Please help me where am i going wrong.
我是 Java 新手,并通过在线访问各种链接尝试了所有这些。请帮助我哪里出错了。
回答by NINCOMPOOP
You can register the created font with the graphics environment , as below :
您可以将创建的字体注册到图形环境中,如下所示:
try {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("akshar.TTF"));
} catch (IOException|FontFormatException e) {
//Handle exception
}
Refer the Java tutorial.
请参阅Java 教程。
回答by Theresa Gamit
Put your ttf into the assests folder :)
将您的 ttf 放入资产文件夹:)