2013-05-18 182 views
3

標題不允許我說的問題,所以實際的錯誤信息是 -使用自定義字體[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。 –

回答

3

當你有可能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

不用擔心隊友,歡呼! –

4

在某些情況下,原因是運行實例無法寫入Java臨時目錄(java.io.tmpdir)。

如果你在tomcat上運行它,也許你刪除了tomcat安裝的臨時目錄,或者這個文件夾有錯誤的權限。

(Tomcat的文件夾)/溫度

+0

這解決了這個問題!誤導性異常消息!謝謝!!! – allancth

0

,你可以嘗試安裝 「DEJAVU的SAN-字體」 和fontconfig,它的工作原理