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

来源:互联网 发布:qq钓鱼软件生成器 编辑:程序博客网 时间:2024/06/05 06:09

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;    }


0 0
原创粉丝点击