public void mouseClicked(MouseEvent e){\x0d\x0aObject source=e.getSource();//获取事件源,即地鼠标签\x0d\x0aif(source instanceof JLabel){//如果事件是标签组件\x0d\x0aJLabel mouse=(JLabel)source;//强制转换为JLabel标签\x0d\x0amouse.setIcon(null);//取消标签图标\x0d\x0a}\x0d\x0a}\x0d\x0a});\x0d\x0athis.getContentPane().add(mouses[i]);//添加显示地鼠的标签到窗体\x0d\x0a}\x0d\x0a\x0d\x0amouses[0].setLocation(253, 300);//设置每个标签的位置\x0d\x0amouses[1].setLocation(333, 250);\x0d\x0amouses[2].setLocation(388, 296);\x0d\x0amouses[3].setLocation(362, 364);\x0d\x0amouses[4].setLocation(189, 353);\x0d\x0amouses[5].setLocation(240, 409);\x0d\x0a\x0d\x0afinal JLabel backLabel=new JLabel();//创建显示背景的标签\x0d\x0abackLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());\x0d\x0athis.setBounds(100,100,img.getIconWidth(),img.getIconHeight());\x0d\x0abackLabel.setIcon(img);//添加背景到标签\x0d\x0athis.getContentPane().add(backLabel);//添加背景标签到窗体\x0d\x0a}\x0d\x0a/**\x0d\x0a*线程的核心方法\x0d\x0a*/\x0d\x0a\x0d\x0apublic void run(){\x0d\x0awhile(true){//使用无限循环\x0d\x0atry{\x0d\x0aThread.sleep(3000);//使线程休眠3秒\x0d\x0aint index=(int)(Math.random()*6);//生成随机的地鼠索引\x0d\x0aif(mouses[index].getIcon()==null){//如果地鼠标签没有设置图片\x0d\x0amouses[index].setIcon(imgMouse);//为该标签添加地鼠图片\x0d\x0a}\x0d\x0a}catch(InterruptedException e){\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}
public void mouseClicked(MouseEvent e){
Object source=e.getSource();//获取事件源,即地鼠标签
if(source instanceof JLabel){//如果事件是标签组件
JLabel mouse=(JLabel)source;//强制转换为JLabel标签
mouse.setIcon(null);//取消标签图标
}
}
});
this.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());
this.setBounds(100,100,img.getIconWidth(),img.getIconHeight());
backLabel.setIcon(img);//添加背景到标签
this.getContentPane().add(backLabel);//添加背景标签到窗体
}
/**
*线程的核心方法
*/
public void run(){
while(true){//使用无限循环
try{
Thread.sleep(3000);//使线程休眠3秒
int index=(int)(Math.random()*6);//生成随机的地鼠索引
if(mouses[index].getIcon()==null){//如果地鼠标签没有设置图片
mouses[index].setIcon(imgMouse);//为该标签添加地鼠图片
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
上一篇:Java猜拳小游戏知识点
下一篇:java基础知识可以做游戏吗