jar中无法运行,估计是找不到图片
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends Frame
{
private static final long serialVersionUID = 1L;
static Image aquariumImage;
public Test()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}
public static void main(String[] args)
{
Test test = new Test();
test.go();
test.setSize(600, 435);
test.setVisible(true);
Graphics g = test.getGraphics();
while (!g.drawImage(aquariumImage, 0, 0, test));
}
public void go()
{
String address = getClass().getResource( "bubbles.gif ").getPath().toString();
System.out.println(address);
aquariumImage = Toolkit.getDefaultToolkit().getImage(address);
}
}
为了teacher assistant,说应该用URL方式调用图片,不确定这里是否是用了URL方式,如果不是又是怎样?本程序eclipse中运行正常(随便用一张gif图片) ,但等打成jar包后就无法运行了,还听说很多人都碰到类似问题了,期待高手给予帮助。
------解决方案--------------------你自己可以判断一下有没有取到图片呀
System.out.println(address);
打印出来的是什么
aquariumImage = Toolkit.getDefaultToolkit().getImage(address);
看看这行完了后aquariumImage是不是null
用URL改成这样
URL address = getClass().getResource( "/bubbles.gif);
aquariumImage = new ImageIcon(address).getImage();
注意
bubbles.gif放到class根目录下