문제

When I try to set a JLabel's font by a font that is returned from Font.createFont, JLabel displays nothing:

Font tempFont = Font.createFont(Font.TRUETYPE_FONT, new File("Path to a ttf font"));
Font font =  tempFont.deriveFont(48);
JFrame frame = new JFrame();
JLabel text = new JLabel("Salam Jahan!");
text.setFont(font);
frame.getContentPane().add(text);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

What is the problem?

도움이 되었습니까?

해결책

deriveFont specifys the font style rather than the size so the loaded font retains its default size of 0

Font font = tempFont.deriveFont(48);

You want the overloaded version of the method

Font font = tempFont.deriveFont(48f);
                                  ^
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top