当前位置:编程学习 > JAVA >>

JAVA初学者求改错

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import java.lang.Runnable;
public  class Shrewmouse extends JFrame implements Runnable {
 private JLabel[] mouses;           
 private ImageIcon imgMouse;   
public Shrewmouse(){
super();
setResizable(false);
getContentPane().setLayout(null);
setTitle("简易打地鼠游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img=new ImageIcon(getClass().getResource("background.jpg"));
    imgMouse=new ImageIcon(getClass().getResource("mouse.png"));
mouses=new JLabel[6];
for(int i=0;i<6;i++){
mouses[i].setSize(imgMouse.getIconWidth(),imgMouse.getIconHeight());
mouses[i].addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){
Object source=e.getSource();
if(source instanceof JLabel){
JLabel mouse=(JLabel)source;
mouse.setIcon(null);
}

}
});
getContentPane().add(mouses[i]);}
mouses[0].setLocation(253,300);
mouses[1].setLocation(333,250);
mouses[2].setLocation(388,296);
mouses[3].setLocation(362,364);
mouses[4].setLocation(189,353);
mouses[5].setLocation(240,409);
final JLabel backLabel=new JLabel();
backLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
setBounds(100,100,img.getIconWidth(),img.getIconHeight());
backLabel.setIcon(img);
getContentPane().add(backLabel);

}
public void run(){
while(true){
try{
Thread.sleep(1000);
int index=(int)(Math.random()*6);
if(mouses[index].getIcon()==null)
mouses[index].setIcon(imgMouse);
}catch (InterruptedException e){
}
}


}
public static void main(String args[]){
  EventQueue.invokeLater(new Runnable(){
   public void run(){
    try{                      
     Shrewmouse frame=new Shrewmouse();  
     frame.setVisible(true);            
     new Thread(frame).start();  
    }catch(Exception e){
     e.printStackTrace();
    }
   }
  });
 }






}
程序运行不起来;语法好像没错; java --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 提示什么错误? --------------------编程问答-------------------- 首先确保图片位置放对
mouses = new JLabel[6];
for (int i = 0; i < 6; i++)
{
mouses[i] = new JLabel();//这里先对mouses中的元素初始化。
……
--------------------编程问答-------------------- 初学者就这么厉害了啊~
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,