我正在使用Java创建数字时钟,并且已经做到了。
现在,我想在显示时间的JLabel
中设置数字时钟的字体。
我在以下代码的帮助下应用字体。
try {
Font f1= new Font("Digital-7" ,Font.BOLD,28);
jLabel1.setFont(f1);
} catch(Exception ex) {
ex.printStackTrace();
}
最佳答案
+1个HovercrafFullOfEels评论。
我不认为我已经预装了Digital-7字体(至少在Windows OS上不是这样),也许您需要font file,而不是加载字体文件并且只能使用它。
请参阅下文以获取字体文件并使用它(为了便于阅读,我省略了错误处理):
String filename="path/to/file/whatever.ttf";//this is for testing normally we would store the font file in our app (knows as an embedded resource), see this for help on that http://stackoverflow.com/questions/13796331/jar-embedded-resources-nullpointerexception/13797070#13797070
Font font = Font.createFont(Font.TRUETYPE_FONT, new File(filename));
font = font.deriveFont(Font.BOLD,28);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
JLabel l = new JLabel("Some Text");
l.setFont(font);