使用自定义字体[java.io.IOException异常:错误读取字体数据]

问题描述:

标题不允许我说的问题,所以实际的错误信息是 -使用自定义字体[java.io.IOException异常:错误读取字体数据]

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不能被解析为一个变量(因为它是在不同的类。文件)。

我怎么能再次解决这个问题?感谢您的帮助。

+1

代替'AddFont.class.getResourceAsStream(FONT_PATH_TELEGRAFICO)的''试试主题。 currentThread()。getContextClassLoader()。的getResourceAsStream(FONT_PATH_TELEGRAFICO)'。 – Perception

+0

@Perception好,那么我该怎么办第二部分,这是 字体telegraficoFont = Font.createFont(Font.TRUETYPE_FONT,telegraficoFontAdd); 该代码删除变量telegraficoFontAdd。 – Hathor

+0

使用完整的包路径来访问字体,例如/com/mydomain/imageFolder/TELEGRAFICO.TTF。 –

当你有可能font file locatingfont stream creation问题,

试试这个 >>Issue loading custom fonthttp://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 
     }); 
    } 
} 
+0

试过这个,不得不在我的项目文件中做一些编辑工作,并且它可以工作。谢谢! – Hathor

+0

不用担心队友,欢呼! –

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

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

(Tomcat的文件夹)/温度

+0

这解决了这个问题!误导性异常消息!谢谢!!! – allancth

,你可以尝试安装 “DEJAVU的SAN-字体” 和fontconfig,它的工作原理