开发者

How to set a custom font's size and other attributes (bold, italic, etc) in Java SWING

Usually, when I initialize the fonts I want to use in my SWING applications, I do it this way:

public static final Font TITLEFONT = new Font("Calibri", Font.BOLD, 40);

Now, I have to do it a bit differently since I'm using some custom fonts from a .ttf file. I initialize the font this way:

try
{
    InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
    TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is);
}
catch (Exception ex)
{
    ex.printStackTrace();
    System.err.println("Font not loaded.  Using Calibri font.");
    TITLEFONT = new Font("Calibri", Font.BOLD, 40);
}

I'm pretty sure it initializes it correctly (I can't tell for sure sinc开发者_Python百科e it is too small for me to see), but I'd like to know how I can manually set the font's size (and if it's bold / other attributes) when loading a font this way.

Thanks a lot in advance!


createFont returns a Font and you can call deriveFont(...) on this, passing in a float for the point size, or an int and float for Font style and point size. I cannot say whether it will work for your particular situation, but it's worth a try.

e.g.,

InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 40f);


I'd simply use:

Font.ITALIC

Font.BOLD

Font.PLAIN

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜