Java字体很不稳定。
这是我目前的编码,这只是一个简单的方法,从文件中获取字体,然后返回它,但我遗漏了一些东西,我只是不知道什么......有人可以帮忙吗?
private Font createFont(){
try {
URL fontUrl = getClass().getClassLoader().getResource("res/Fonts/aesysmatt.ttf");
Font mFont = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
return mFont;
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
return null;
}
以下是我的错误:
java.lang.NullPointerException
at disparity.characterCreation.ColoredButton.createFont(ColoredButton.java:21)
at disparity.characterCreation.ColoredButton.<init>(ColoredButton.java:37)
at disparity.characterCreation.StartScreen.initialize(StartScreen.java:93)
at disparity.characterCreation.StartScreen.<init>(StartScreen.java:61)
at disparity.characterCreation.StartScreen$2.run(StartScreen.java:48)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:0)
嗯,我不确定我是怎么做到的,但是一旦修好了,最后就出现了错误行,希望对某人有所帮助。
URL fontUrl = getClass().getResource("res/Fonts/aesymatt.ttf");
一定是因为它在原版上加载了classLoader,这实际上没有任何意义。