Using a custom font for a JLabel

前端 未结 2 1535
感动是毒
感动是毒 2020-12-20 06:56

I\'m trying to use a special font in my JFrame, but I\'m running into problems. I have a JLabel defined like this:

private JLabel lab = new JLabel(\"Text\");         


        
2条回答
  •  隐瞒了意图╮
    2020-12-20 07:15

    The Font you created has to be registered first in the GraphicsEnvironment to be accessible to all and derive the size of the font:

    Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("/CUSTOMFONT-MEDIUM.TTF").openStream());   
    
    GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    genv.registerFont(font);
    // makesure to derive the size
    font = font.deriveFont(12f);
    

提交回复
热议问题