Jpanel 背景图片不能显示

package窗口;importjavax.swing.*;importjava.awt.*;importjava.net.*;publicclassbeijing{JF... package 窗口;

import javax.swing.*;
import java.awt.*;
import java.net.*;
public class beijing {
JFrame f;
Container c;
public beijing(){
f=new JFrame("背景");
f.setSize(400,300);
c=f.getContentPane();
c.setLayout(new BorderLayout());
newjpanl p=new newjpanl();
c.add(p,BorderLayout.SOUTH);
p.add(new JTextField(10));
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new beijing();

}

}
class newjpanl extends JPanel{
public void paintComponent(Graphics g){
int x=0,y=0;
URL url=getClass().getResource("C:/Documents and Settings/Administrator/桌面/000.gif");
ImageIcon icon =new ImageIcon(url);
g.drawImage(icon.getImage(), x, y, getSize().width, getSize().height, this);
while(true )
{
g.drawImage(icon.getImage(), x, y, this);
if(x>getSize().width&&y>getSize().height) break;
if(x>getSize().width){x=0;y+=icon.getIconHeight();}
else x+=icon.getIconHeight();
}
}
}

编译问题:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ??.newjpanl.paintComponent(beijing.java:30)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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)
275403088@qq.com 谢谢了
展开
 我来答
2008windflower
2010-04-21 · 超过18用户采纳过TA的回答
知道答主
回答量:30
采纳率:0%
帮助的人:50.2万
展开全部

又是你哈,看来问题还没解决哈。

其实呢,我比较倾向推荐使用相对路径,这样程序才好移植。

如你之前程序里图片路径“/image/123.gif”,“/”指的是当前目录的根目录,当前java文件的目录是“窗口”,它的根目录也就是上一级目录一般为src,所以image这个文件夹便在src下,与“窗口”目录同级。

如图所示,即为image的位置。希望能对你有所帮助。 

这样好了,你把邮箱地址给我,我直接把工程打包发给你,看问题出在什么地方 。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式