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));