ImageIcon不能显示图片

drongHua 发布于 2012/11/23 18:29
阅读 1K+
收藏 0

package javaRealStartTwo;

import java.awt.*;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;
import javax.swing.border.LineBorder;

public class Matching extends JFrame implements MouseListener{
    JLabel img[] = new JLabel[5];
    JLabel targets[] = new JLabel[5];
    Point pressPoint;
 public Matching(){
  super();
  getContentPane().setLayout(new BorderLayout());
  setBounds(100,100,330,175);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setTitle("简单配对游戏");
     JPanel imagePanel = new JPanel();
     imagePanel.setLayout(null);
     imagePanel.setOpaque(false);
     setGlassPane(imagePanel);
     getGlassPane().setVisible(true);
     ImageIcon icon[] = new ImageIcon[5];
     icon[0] = new ImageIcon(getClass().getResource("kafei.PNG"));
     icon[1] = new ImageIcon(getClass().getResource("xianshiqi.PNG"));
     icon[2] = new ImageIcon(getClass().getResource("xiyiji.PNG"));
     icon[3] = new ImageIcon(getClass().getResource("yifu.PNG"));
     icon[4] = new ImageIcon(getClass().getResource("zixingche.PNG"));
     JPanel bottomPanel = new JPanel();
     bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
     getContentPane().add(bottomPanel,BorderLayout.SOUTH);
     for(int i=0;i<5;i++){
      img[i]=new JLabel(icon[i]);
      img[i].setSize(50,50);
      img[i].setBorder(new LineBorder(Color.GRAY));
      int x = (int)(Math.random()*(getWidth()-50));
      int y = (int)(Math.random()*(getHeight()-150));
      img[i].setLocation(x,y);
      img[i].addMouseListener(this);
      img[i].addMouseMotionListener((MouseMotionListener) this);
      imagePanel.add(img[i]);
      
      targets[i]= new JLabel();
      targets[i].setOpaque(true);
      targets[i].setBackground(Color.ORANGE);
      targets[i].setHorizontalTextPosition(SwingConstants.CENTER);
      targets[i].setVerticalTextPosition(SwingConstants.BOTTOM);
      targets[i].setPreferredSize(new Dimension(80,80));
      targets[i].setHorizontalAlignment(SwingConstants.CENTER);
      bottomPanel.add(targets[i]);
     }
     targets[0].setText("咖啡");
     targets[1].setText("显示器");
     targets[2].setText("洗衣机");
     targets[3].setText("衣服");
     targets[4].setText("自行车");
     setSize(600,450);
  setVisible(true);
 }
 
 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }


 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }


 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub
  
 }
 public void mousePressed(MouseEvent e) {
  // TODO Auto-generated method stub
  pressPoint = e.getPoint();
 }
 public void mouseReleased(MouseEvent e) {
  if(check()){
   getGlassPane().setVisible(false);
     for(int i=0;i<5;i++){
     targets[4].setText("配对成功");
     targets[i].setIcon(img[i].getIcon());
     }
  }
  
 }
 
 private boolean check(){
  boolean result = true;
   for(int i=0;i<5;i++){
    Point location = img[i].getLocationOnScreen();
    Point seat = targets[i].getLocationOnScreen();
    targets[i].setBackground(Color.GREEN);
    if (location.x<seat.x || location.y<seat.y || location.x > seat.x+80
    || location.y > seat.y + 80){
     targets[i].setBackground(Color.ORANGE);
     result = false;
    }
   }
   return result;
 }
 
 
 public void mouseDragged(MouseEvent e){  //处理鼠标拖动事件
  JLabel source = (JLabel)e.getSource();
  Point imgPoint = source.getLocation();
  Point point = e.getPoint();
  source.setLocation(imgPoint.x+point.x-pressPoint.x,imgPoint.y+point.y-pressPoint.y);
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub;
  Matching a = new Matching();
 }

}

###########################

运行后有如下问题:

Exception in thread "main" java.lang.NullPointerException
 at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
 at javaRealStartTwo.Matching.<init>(Matching.java:27)
 at javaRealStartTwo.Matching.main(Matching.java:123)
我是用Eclipse

图片与程序的位置在D:\workspace\Java教程\src\javaRealStartTwo

为什么程序运行不了,我把图片和程序放在一起了,不知道那里有问题,请帮忙指出一下,谢谢

加载中
0
刘学炜
刘学炜
路径的问题,初学者对路径不熟悉的话,建议在工程目录下新建一个image的文件夹的方式来处理
0
drongHua
drongHua

那现在我的代码是没有问题吗?

  icon[0] = new ImageIcon(getClass().getResource("kafei.PNG"));
     icon[1] = new ImageIcon(getClass().getResource("xianshiqi.PNG"));
     icon[2] = new ImageIcon(getClass().getResource("xiyiji.PNG"));
     icon[3] = new ImageIcon(getClass().getResource("yifu.PNG"));
     icon[4] = new ImageIcon(getClass().getResource("zixingche.PNG"));

要不要修改呢?

返回顶部
顶部