java小笔记----awt 使用自定义字体

1,假设字体文件为 “宋体.ttf”

private static Font getSelfDefinedFont(String filename)
    {
        Font font = null;
        //字体文件在conf下面
        String filepath = RandomCodeChinese.class.getResource("/").getFile().replaceAll("%20", " ") + "../conf/" + filename;
        File file = new File(filepath);
        try
        {
            FileInputStream fi = new FileInputStream(file);
            BufferedInputStream fb = new BufferedInputStream(fi);
            font = Font.createFont(Font.TRUETYPE_FONT, fb);
        }
        catch (FontFormatException e)
        {
            return null;
        }
        catch (FileNotFoundException e)
        {
            return null;
        }
        catch (IOException e)
        {
            return null;
        }
        if("宋体.ttf".equals(filename))
        {
        	font = font.deriveFont(Font.BOLD, 25);
        }
        else
        {
        	font = font.deriveFont(Font.PLAIN, 25);
		}
        return font;
    }


你可能感兴趣的:(java小笔记----awt 使用自定义字体)