当类位于包中时加载 GUI 图像时出现问题
我已经用 Java 编程有一段时间了,但从未真正使用过 swing 包。我目前正在为人工智能呼叫和响应程序设计一个 GUI,尽管我所做的其余工作相对复杂(至少对我来说),但这个看起来非常容易实现的图像加载问题却困扰着我。
如果不在包中,下面的类也可以工作,这才是真正让我困惑的地方。我尝试了几种不同的实现建议(一种来自 Head First Java,一种来自 docs.oracle.com 教程,另一种使用 http://leepoint.net/notes-java/GUI-lowlevel/graphics/45imageicon.html 建议)。
package CaRII;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class P{
public static void main(String [] args){
P p = new P();
p.go();
}
public void go(){
JFrame frame = new JFrame("CaRRI: Call and Response Intelligent Improviser");
PBackground mainPanel = new PBackground();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(800,500);
frame.setVisible(true);
}
就像我说的,
package CaRII;
import java.awt.*;
import javax.swing.*;
public class PBackground extends JPanel{
public Image backgroundImage;
public PBackground(){
backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");
}
public PBackground(LayoutManager layout){
backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");
}
public void paint(Graphics g){
g.drawImage(backgroundImage,0,0,null);
}
}
奇怪的是,如果这两个类位于 CaRRI 包中,它不会显示图像;但是如果我在没有包声明的情况下编译并运行它们,它们运行得很好(尽管图像在窗口调整大小之前不会加载......但我已经在网上看到了解决方案,所以一旦我在包裹)。我一直在 XCode 和 JEdit 中编写,图像存储在源代码(/src/CaRII/P.java ... /src/CaRII/CaRIIBackGround.jpg)内的包文件夹中,我也尝试将图像存储在/src/ 中的 resources 文件夹并使用,
ImageIcon(getClass().getResource("/resources/CaRIIBackGround.jpg)).getImage();
但这会在运行时导致另一个错误
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at CaRII.PBackground.<init>(PBackground.java:19)
at CaRII.P.go(P.java:21)
at CaRII.P.main(P.java:15)
任何帮助将不胜感激,因为尽管它很简单,但这整个早上都困扰着我,而且我还有很多其他类要编写。
谢谢 (这是图片(我是新用户,所以我无法发布图片,但这是它的链接)) http://imageshack.us/photo/my-images/189/cariibackground.jpg
I've been programming in Java for a little while now but have never really done much with the swing packages. I am currently designing a GUI for an A.I. call and response program despite the relative complexity (for me at least) of the rest of what I have been doing, this image loading problem, which seemed extremely simple to implement is stumping me.
The below classes work if not in a package, which is what really confuses me. I've tried several different implementation suggestions (one from Head First Java, one from the docs.oracle.com tutorials and another using what http://leepoint.net/notes-java/GUI-lowlevel/graphics/45imageicon.html suggests).
package CaRII;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class P{
public static void main(String [] args){
P p = new P();
p.go();
}
public void go(){
JFrame frame = new JFrame("CaRRI: Call and Response Intelligent Improviser");
PBackground mainPanel = new PBackground();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(800,500);
frame.setVisible(true);
}
}
package CaRII;
import java.awt.*;
import javax.swing.*;
public class PBackground extends JPanel{
public Image backgroundImage;
public PBackground(){
backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");
}
public PBackground(LayoutManager layout){
backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");
}
public void paint(Graphics g){
g.drawImage(backgroundImage,0,0,null);
}
}
Like I said the strange thing is that it doesn't display the image if these two classes are in package CaRRI; but if I compile and run them without the package declaration they run fine(albiet the image not loading until the window resizes... but i have seen solutions online for that so I will be able to sort that once I get it loading within the package). I have been writing in XCode and JEdit and the image is stored within the package folder inside source (/src/CaRII/P.java ... /src/CaRII/CaRIIBackGround.jpg), I have also tried storing the image in a resources folder within /src/ and using
ImageIcon(getClass().getResource("/resources/CaRIIBackGround.jpg)).getImage();
but that that causes another error when run
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at CaRII.PBackground.<init>(PBackground.java:19)
at CaRII.P.go(P.java:21)
at CaRII.P.main(P.java:15)
Any help would be much appreciated as despite its simplicity this has been stumping me all morning and I have a lot of other classes to write.
Thanks
(Heres the image(im a new user so i cant post images but this is a link to it))
http://imageshack.us/photo/my-images/189/cariibackground.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码对我有用:
封装CaRII;
但要小心,当我生成 jar 文件时,由于图像名称中的大写,我遇到了与您相同的错误(线程“main”java.lang.NullPointerException 中的异常)。如果您使用 Windows,则应仅对资源使用小写名称。
问候,
This code works for me :
package CaRII;
But be carefull I've got the same error (Exception in thread "main" java.lang.NullPointerException) than you when I generate my jar file because of Upper case in the image name. You should use only lower case names for your resources if you use windows.
Regards,
检查此链接,这必须是您的目录结构,用于放置图像,也请检查代码示例。 我的项目。我刚刚创建了这个项目,让您知道,如果您手动执行此操作,事情需要如何进行,如何进行访问。
Check this Link out, this has to be your Directory Structure, for placing Images, Do check the code example too. MY PROJECT. I just created this project for you to know, how things need to be, to be accessed, if you doing that manually.