项目预览:
设置字体的代码:SetFont.java
public class SetFont {
Font font=null;
public Font getFont(){
try {
font=Font.createFont(Font.TRUETYPE_FONT,new File("src/font/DS-DIGIT.TTF"));
font=font.deriveFont(80.0F);//SIZE
font=font.deriveFont(Font.BOLD);//Style
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (FontFormatException e) {
JOptionPane.showMessageDialog(null, "加载字体出错", "系统消息",JOptionPane.INFORMATION_MESSAGE);
font=new Font("Verdana",Font.BOLD,18);
}
return font;
}
}
调用字体:
public class MainFrame(){
public MainFrame(){
private JLabel time=new JLabel("2012");
time.setFont(new SetFont().getFont());
time.setForeground(Color.RED);
getContentPane().add(time);
time.setBounds(0,0,30,40);
setSize(200,300);
setTitle("lovemu");
setVisible(true);
}
public static void main(String[] args){
new MainFrame();
}
}
效果为:
还可以使用网络字体:
URL url=new URL("http://www.fonts.com/Digital.ttf");
InputStream in=url.openStream();
Font f=Font.createFont(Font.TRUETYPE_FONT,in);