自定义字体不显示/非常小?

时间:2018-03-17 12:55:51

标签: java swing fonts

我正在尝试使用Google字体中的字体。 FontFormatException告诉我“读取字体数据有问题”,这似乎导致非常小的文本。我的空框架上有一条细细的黑线,我猜这是我的标签。不知道为什么我得到这个例外。我尝试了一堆不同的字体,全部来自谷歌字体,问题总是一样的。

public class View extends JFrame implements MouseListener {
    private JPanel content;
    private Font font;

    public View(){

        content = new JPanel();       
        content.setPreferredSize(new Dimension(500, 500));

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(content);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        try {
            InputStream file = Canvas.class.getResourceAsStream("RammettoOne-Regular.ttf");
            font = Font.createFont(Font.TRUETYPE_FONT, file);

            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(font);

            font.deriveFont(Font.PLAIN, 25);

        } catch (IOException | FontFormatException ex) {
            System.out.println(ex.getMessage());
        }

        addLabel();
        revalidate();
    }

    public void addLabel(){
        JLabel label = new JLabel("this is a test");
        label.setFont(font);
        label.setForeground(Color.BLACK);
        content.add(label);
    }
}

我尝试在deriveFont中使字体变大,但没有变化。 知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

font.deriveFont(Font.PLAIN, 25);

需要:

// assign the derived (resized) font to the local attribute reference! 
font = font.deriveFont(Font.PLAIN, 25);

结果

enter image description here

代码

以下是显示上述屏幕截图的MCVE。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;
import java.io.*;

public class TestRammettoFont {

    private JComponent ui = null;
    String fontAddress = "jar:http://dl.1001fonts.com/rammetto-one.zip"
            + "!/RammettoOne-Regular.ttf";

    TestRammettoFont() {
        try {
            initUI();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public final void initUI() throws Exception {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(14, 14, 14, 14));

        URL url = new URL(fontAddress);
        InputStream is = url.openStream();
        Font font = Font.createFont(Font.TRUETYPE_FONT, is);

        font = font.deriveFont(Font.PLAIN, 25);
        JLabel l = new JLabel("The quick brown fox (etc.)");
        l.setFont(font);
        ui.add(l);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TestRammettoFont o = new TestRammettoFont();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
相关问题