使用自定义字体 [java.io.IOException:读取字体数据时出错。]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16621750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Custom Fonts [java.io.IOException: Error reading font data.]
提问by Hathor
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.run(MainFrame.java:105)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access 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
});
}
0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
The code is -
代码是——
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.");
}
}
}
}
and the file that I used to get the AddFont addFont-
以及我用来获取 AddFont addFont- 的文件
public static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";
I was instructed to make a new thread because this is a separate problem from my other one.
我被指示创建一个新线程,因为这是与我的另一个问题不同的问题。
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
为什么我会遇到这个问题,我该如何解决?我的 imageFolder 中有我的 TELEGRAFICO.TTF 字体,它实际上只是我的资源文件夹。我用
regButton = new JButton();
regButton.setText("Foo");
regButton.setAlignmentX(Component.CENTER_ALIGNMENT);
regButton.setFont(telegraficoFont);
to call in my path.
呼唤我的道路。
What am I doing wrong?
我究竟做错了什么?
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 -
(我想在多个类文件中的按钮上使用该字体。我在这里尝试使用它 -
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;
}
}
But it said telegraficoFont cannot be resolved to a variable. (Because it was in a different class file.)
但它说telegraficoFont 不能解析为变量。(因为它在不同的类文件中。)
How can I fix this? Thanks again for the help.
我怎样才能解决这个问题?再次感谢您的帮助。
采纳答案by Ravi Trivedi
As you have a problem with possible font file locating
and font stream creation
,
由于您对可能的font file locating
和有问题font stream creation
,
Try this>> Issue loading custom fontANDhttp://forums.devshed.com/showpost.php?p=2268351&postcount=2
试试这个>>问题加载自定义字体和http://forums.devshed.com/showpost.php?p=2268351&postcount=2
To answer your question "how to make this function easy to use everywhere"
, do as this:
要回答您的问题"how to make this function easy to use everywhere"
,请执行以下操作:
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
});
}
}
And then in your calling class:
然后在您的通话课程中:
##代码##回答by Steven Lizarazo
In some cases the cause is the running instance not being able to write to the Java temp directory (java.io.tmpdir).
在某些情况下,原因是正在运行的实例无法写入 Java 临时目录 (java.io.tmpdir)。
If your are running it on tomcat maybe you deleted the temp directory of the tomcat installation, or the folder have wrong permissions.
如果您在 tomcat 上运行它,可能您删除了 tomcat 安装的临时目录,或者文件夹权限错误。
(tomcat folder)/temp
(tomcat 文件夹)/temp
回答by Eric Lee
you could try to install "dejavu-sans-fonts" and fontconfig, it works
你可以尝试安装“dejavu-sans-fonts”和fontconfig,它可以工作