2
我想将JLabel的字体设置为自定义字体。读取文件时不会抛出异常,但在拨打label.setText("string")
时不会出现任何异常。文字出现在我注释掉label.setFont(f)
行时。任何人都知道我在做什么错了?此代码位于JPanel类中。使用自定义字体
_mineLabel = new JLabel();
_timeLabel = new JLabel();
try {
Font f = Font.createFont(Font.TRUETYPE_FONT,new File("/Users/simon/Documents/workspace/Minesweeper/bin/minesweeper/DS-DIGI.TTF"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);
f.deriveFont(12f);
_mineLabel.setFont(f);
_timeLabel.setFont(f);
} catch(IOException e) {
e.printStackTrace();
} catch(FontFormatException e) {
e.printStackTrace();
}
this.add(_mineLabel);
this.add(_timeLabel);
_timeLabel.setText("test");
尝试'F = f.deriveFont(12F)' – nachokk
感谢nachokk!那解决了它。 –