我已经用Java编程了一段时间但是从来没有真正使用swing包.我目前正在为A.I.设计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 /中使用的资源文件夹
ImageIcon(getClass().getResource("/resources/CaRIIBackGround.jpg)).getImage();
但这会在运行时导致另一个错误
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(ImageIcon.java:181)
at CaRII.PBackground.(PBackground.java:19)
at CaRII.P.go(P.java:21)
at CaRII.P.main(P.java:15)
任何帮助都会非常感激,尽管它很简单,但是我整个上午一直困扰着我,还有很多其他的课程要写.
最佳答案
package CaRII;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class P{
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
P p = new P();
p.go();
}
});
}
public void go(){
try {
JFrame frame = new JFrame("CaRRI: Call and Response Intelligent Improviser");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
PBackground mainPanel = new PBackground();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
//frame.setSize(800,500);
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setLocationByPlatform(true);
frame.setVisible(true);
} catch(Exception e) {
e.printStackTrace();
}
}
}
class PBackground extends JPanel{
public BufferedImage backgroundImage;
public PBackground() throws Exception {
URL url = new URL("http://desmond.imageshack.us/Himg189/" +
"scaled.php?server=189&filename=cariibackground.jpg&res=medium");
// You might form that URL using something like..
//URL url = this.getClass().getResource("/CaRIIBackGround.jpg");
backgroundImage = ImageIO.read(url);
Dimension d = new Dimension(
backgroundImage.getWidth(),
backgroundImage.getHeight());
setPreferredSize(d);
}
/* What was this supposed to achieve?
public PBackground(LayoutManager layout){
backgroundImage = Toolkit.getDefaultToolkit().createImage("CaRIIBackGround.jpg");
}
*/
// Don't override paint() in a Swing panel!
//public void paint(Graphics g){
@Override
public void paintComponent(Graphics g) {
// USE the ImageObserver!
//g.drawImage(backgroundImage,0,0,null);
g.drawImage(backgroundImage,0,0,getWidth(),getHeight(),this);
}
}