标题不允许我说问题,所以实际的错误信息是 -
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)
Run Code Online (Sandbox Code Playgroud)
代码是 -
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
});
}
Run Code Online (Sandbox Code Playgroud)
和我用来获取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.");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我被指示创建一个新线程,因为这是另一个问题.
为什么我会遇到这个问题,我该如何解决?我的imageFolder中有我的TELEGRAFICO.TTF字体,它实际上只是我的资源文件夹.我用
public static final String FONT_PATH_TELEGRAFICO = "imageFolder/TELEGRAFICO.TTF";
Run Code Online (Sandbox Code Playgroud)
打电话给我的路.
我究竟做错了什么?
编辑 - 我不再收到该错误消息,我没有得到"未加载字体".如何在除我制作该方法之外的其他类文件中使用该字体?
(我想在多个类文件的按钮上使用该字体.我尝试在这里使用它 -
regButton = new JButton();
regButton.setText("Foo");
regButton.setAlignmentX(Component.CENTER_ALIGNMENT);
regButton.setFont(telegraficoFont);
Run Code Online (Sandbox Code Playgroud)
但它表示telegraficoFont无法解析为变量.(因为它在不同的类文件中.)
我怎样才能解决这个问题?再次感谢您的帮助.
由于您对可能的font file locating和有问题font stream creation,
试试这个>>问题加载自定义字体 和 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的通话课程中:
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
});
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,原因是正在运行的实例无法写入Java临时目录(java.io.tmpdir).
如果你在tomcat上运行它可能你删除了tomcat安装的临时目录,或者文件夹有错误的权限.
(tomcat文件夹)/ temp
在某些情况下,可能您的运行环境中缺少Fontconfig 。安装后,一切正常。
例如,
yum install fontconfig
Run Code Online (Sandbox Code Playgroud)