public static Font ttf2font(String filePath, int fontStyle, int fontSize) {
File fontFile = new File(filePath);
Font font = null;
try {
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
font = font.deriveFont(fontStyle, fontSize);
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return font;
}
类似这样使用:
t.setFont(TextArea.ttf2font("./Consolas.ttf", Font.BOLD, 20));
本文介绍如何使用Java创建并定制Consolas字体,通过Font.createFont方法读取TrueType字体文件,并设置样式和大小。遇到异常时进行了错误处理。
1516

被折叠的 条评论
为什么被折叠?



