2

I am creating a digital clock using Java and I have done that. Now I want to set the font of digital clock in the JLabel which is showing the time. I am applying the font with the help of following code.

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

1 回答 1

9

+1 HovercrafFullOfEels comments.

I dont think I have seen Digital-7 font pre-installed (well at least not on Windows OS) perhaps you need the font file, you would than load the font file and only than can it be used.

See below for loading a font file and than using it (I have omitted error handling for readability):

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);
于 2013-06-29T09:27:17.033 回答