使用自定义字体[java.io.IOException:读取字体数据时出错。]

时间:2013-05-18 07:20:19

标签: java text fonts

标题不允许我说问题,所以实际的错误信息是 -

java.io.IOException: Problem reading font data.
at java.awt.Font.createFont(Unknown Source)
at AddFont.createFont(AddFont.java:11)
at MainFrame$1.run(MainFrame.java:105)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

代码是 -

     public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
 public void run() {
    try {
        AddFont addFont = new AddFont();
        addFont.createFont();
    } catch (Exception e) {
        e.printStackTrace();
    }
    createGUI();

 } //public void run() Closing
});
}

和我用来获取AddFont addFont的文件 -

import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;


public class AddFont extends MainFrame{
public void createFont(){
Font ttfBase = null;
    Font telegraficoFont = null;{
try {
    InputStream myStream = new BufferedInputStream(new FileInputStream(FONT_PATH_TELEGRAFICO));
    ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
    telegraficoFont = ttfBase.deriveFont(Font.PLAIN, 24);
} catch (Exception ex) {
    ex.printStackTrace();
    System.err.println("Font not loaded.");
}
}
}
}

我被指示创建一个新线程,因为这是另一个问题。

为什么我会遇到这个问题,我该如何解决? 我的imageFolder中有我的TELEGRAFICO.TTF字体,它实际上只是我的资源文件夹。我用

   public static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";

在我的路上打电话。

我做错了什么?

编辑 - 我不再收到该错误消息,并且我没有收到“未加载字体”。如何在除我使用该方法之外的其他类文件中使用该字体?

(我想在多个类文件的按钮上使用该字体。我尝试在这里使用它 -

regButton = new JButton();
regButton.setText("Foo");
regButton.setAlignmentX(Component.CENTER_ALIGNMENT);
regButton.setFont(telegraficoFont);

但它说telegraficoFont无法解析为变量。 (因为它在不同的类文件中。)

我该如何解决这个问题?再次感谢您的帮助。

3 个答案:

答案 0 :(得分:5)

在某些情况下,原因是正在运行的实例无法写入Java临时目录(java.io.tmpdir)。

如果你在tomcat上运行它,你可能删除了tomcat安装的临时目录,或者文件夹有错误的权限。

(tomcat文件夹)/ temp

答案 1 :(得分:3)

因为您可能遇到问题font file locatingfont stream creation

试试这个>> Issue loading custom font AND http://forums.devshed.com/showpost.php?p=2268351&postcount=2

要回答您的问题"how to make this function easy to use everywhere",请执行以下操作:

    public class AddFont extends MainFrame {

    private static Font ttfBase = null;
    private static Font telegraficoFont = null;
    private static InputStream myStream = null;
    private static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";

    public Font createFont() {


            try {
                myStream = new BufferedInputStream(
                        new FileInputStream(FONT_PATH_TELEGRAFICO));
                ttfBase = Font.createFont(Font.TRUETYPE_FONT, myStream);
                telegraficoFont = ttfBase.deriveFont(Font.PLAIN, 24);               
            } catch (Exception ex) {
                ex.printStackTrace();
                System.err.println("Font not loaded.");
            }
            return telegraficoFont;
    }
}

然后在你的通话类中:

    public class Test {

    public static Font font = null;

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    if (font == null) {
                        font = AddFont.createFont();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                createGUI();

            } // public void run() Closing
        });
    }
}

答案 2 :(得分:0)

您可以尝试安装" dejavu-sans-fonts"和fontconfig,它的工作原理