简体   繁体   中英

Using a custom font for a JLabel

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");

and I have a file called CUSTOMFONT-MEDIUM.TTF (TrueType font) but after writing the following:

    try {
    lab.setFont(Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("/CUSTOMFONT-MEDIUM.TTF").openStream()));
    } catch(IOException ex){
    //exception handled here I suppose  
    } catch(FontFormatException ex2) {
    //same here
    }

the code compiles and the everything works right except that "lab" is not displayed so there is no text. I suppose it is because I never specified what the font size should be, but any attempt I have made to do that fails. Can someone help me out here?

@sasankad is mostly correct (+1).

Once you have created the font, it will have a default size of 1

Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/CUSTOMFONT-MEDIUM.TTF"));  

You then need to derive the font size and style you want.

Font biggerFont = font.deriveFont(Font.BOLD, 48f);

在此处输入图片说明

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCustomFont {

    public static void main(String[] args) {
        new TestCustomFont();
    }

    public TestCustomFont() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            try {
                Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/Royal Chicken.ttf"));
                JLabel happy = new JLabel("Happy little Miss Chicken");
                happy.setFont(font.deriveFont(Font.BOLD, 48f));
                add(happy);
            } catch (FontFormatException | IOException ex) {
                ex.printStackTrace();
            }
        }

    }

}

Check out java.awt.Font for more details...

You may also want to take a look at Physical and Logical Fonts, Font Configuration Files

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM