使用自定义字体[java.io.IOException:读取字体数据时出错. [英] Using Custom Fonts [java.io.IOException: Error reading font data.]

查看:1464
本文介绍了使用自定义字体[java.io.IOException:读取字体数据时出错.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The title doesn't allow me to say Problem, so the actual error message was -

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-

and the file that I used to get the 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.");
}
}
}
}

我被要求制作一个新线程,因为这是与我另一个线程不同的问题.

I was instructed to make a new thread because this is a separate problem from my other one.

为什么会出现此问题,如何解决? 我的imageFolder中有我的TELEGRAFICO.TTF字体,它实际上只是我的资源文件夹.我用

Why am I getting this problem, and how can I fix it? I have my TELEGRAFICO.TTF font in my imageFolder, which is really just my resources folder. I use

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

打电话给我.

我在做什么错了?

编辑-我不再收到该错误消息,也没有收到字体未加载"的信息.除了在其中创建该方法的类文件外,如何在其他类文件中使用字体?

EDIT - I no longer get that error message, and I don't get "Font not loaded". How can I use the font in other class files other than the one I made that method in?

((我想在多个类文件中的按钮上使用该字体.我尝试在此处使用它-

(I want to use that font on buttons in multiple class files. I tried using it here -

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

但是它说telegraficoFont无法解析为变量. (因为它在另一个类文件中.)

But it said telegraficoFont cannot be resolved to a variable. (Because it was in a different class file.)

我该如何解决?再次感谢您的帮助.

How can I fix this? Thanks again for the help.

推荐答案

由于可能存在font file locatingfont stream creation的问题,

As you have a problem with possible font file locating and font stream creation,

尝试 >> >> 正在加载自定义字体 http://论坛. devshed.com/showpost.php?p=2268351&postcount=2

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

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

To answer your question "how to make this function easy to use everywhere", do as this:

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

这篇关于使用自定义字体[java.io.IOException:读取字体数据时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆