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 谢谢了 展开
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 谢谢了 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询