如何在JLabel中设置数字时钟字体?

ade*_*ngh 2 java fonts swing jlabel jbutton

我正在使用Java创建一个数字时钟,我已经做到了.现在我想设置JLabel显示时间的数字时钟字体.我在下面的代码的帮助下应用字体.

try {
    Font f1= new Font("Digital-7" ,Font.BOLD,28);
    jLabel1.setFont(f1);
} catch(Exception ex) {
    ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*amp 8

+1 HovercrafFullOfEels评论.

我不认为我已经预先安装了Digital-7字体(至少不是在Windows操作系统上),也许你需要字体文件,你只需要加载字体文件而不是它可以使用.

请参阅下面的加载字体文件而不是使用它(为了便于阅读,我省略了错误处理):

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);
Run Code Online (Sandbox Code Playgroud)