1 package test;
2
3 import java.awt.EventQueue;
4 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent;
6
7 import javax.swing.ImageIcon;
8 import javax.swing.JFrame;
9 import javax.swing.JLabel;
10 import javax.swing.JOptionPane;
11 /**
12 * 打地鼠游戏
13 * @author mike
14 *
15 */
16 public class Shrewmouse extends JFrame implements Runnable {
17 private JLabel[] mouses;// 存放显示地鼠标签的数组
18 private ImageIcon imgMouse;// 地鼠图片对象
19 private int clickedNumber = 0;// 击中的地鼠数量
20 private long[] jumpTime;// 地鼠跳出的当前时间数组
21
22 public static void main(String[] args) {
23 EventQueue.invokeLater(new Runnable() {
24 @Override
25 public void run() {
26 try {
27 Shrewmouse frame = new Shrewmouse();// 创建窗体
28 frame.setVisible(true);// 显示窗体
29 new Thread(frame).start();// 启动线程
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 }
34 });
35 }
36
37 public Shrewmouse() {
38 super();
39 setResizable(false);// 禁止调整窗体大小
40 getContentPane().setLayout(null);// 设窗体不使用布局管理器
41 setTitle("简易打地鼠游戏");// 标题
42 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43
44 // 初始化背景图像对象
45 ImageIcon img = new ImageIcon(getClass().getResource("background.jpg"));
46 // 初始化地鼠图片对象
47 imgMouse = new ImageIcon(getClass().getResource("mouse.png"));
48 mouses = new JLabel[6];// 创建显示地鼠的标签数组
49 jumpTime = new long[6];// 创建显示地鼠跳出的当前时间数组
50 for (int i = 0; i < mouses.length; i++) {// 遍历数组
51 mouses[i] = new JLabel(); // 初始化每个数组
52 // 设置标签与地鼠图片同样的大小
53 mouses[i].setSize(imgMouse.getIconWidth(), imgMouse.getIconHeight());
54 mouses[i].addMouseListener(new MouseAdapter() {// 为标签添加鼠标事件监听适配器
55 /**
56 * 处理鼠标单击事件的方法
57 */
58 @Override
59 public void mouseClicked(MouseEvent e) {
60 Object source = e.getSource();// 获取事件源,即地鼠标签
61 JLabel mouse = (JLabel) source;
62 if (mouse.getIcon() != null) {// 如果地鼠标签存在地鼠图片,设置为空
63 mouse.setIcon(null);
64 clickedNumber++;// 被击中地鼠数量自增
65 }
66 }
67 });
68 getContentPane().add(mouses[i]);
69
70 }
71 mouses[0].setLocation(253, 300);
72 mouses[1].setLocation(333, 250);
73 mouses[2].setLocation(388, 296);
74 mouses[3].setLocation(362, 364);
75 mouses[4].setLocation(189, 353);
76 mouses[5].setLocation(240, 409);
77
78 final JLabel backLabel = new JLabel();
79 // 设置标签与背景图片相同的大小
80 backLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
81
82 // 设置窗体近似背景图片大小
83 setBounds(100, 100, img.getIconWidth(), img.getIconHeight() + 30);
84 backLabel.setIcon(img);// 添加背景到标签
85 getContentPane().add(backLabel);// 添加背景图片到窗体
86
87 }
88
89 /**
90 * 线程的核心方法
91 */
92 @Override
93 public void run() {
94 while (true) { // 使用无限循环
95 for (int i = 0; i < jumpTime.length; i++) {// 循环地鼠跳出的当前时间数组
96 if (System.currentTimeMillis() - jumpTime[i] > 1000) {// 如果地鼠显示时间超过1秒,设置图片为空
97 mouses[i].setIcon(null);
98 }
99 }
100 try {
101 Thread.sleep(1000);// 使线程休眠1秒
102 int index = (int) (Math.random() * 6);// 生成随机的地鼠索引
103 if (mouses[index].getIcon() == null) {// 如果地鼠索引没有设置图片
104 mouses[index].setIcon(imgMouse);// 为该标签添加地鼠图片
105 jumpTime[index] = System.currentTimeMillis();
106 }
107 if (clickedNumber == 10) {// 如果击中10个地鼠就赢了
108 int result =JOptionPane.showConfirmDialog(this, "<html><body>You Win!!<br/>是否继续?</body></html>", "提示信息", JOptionPane.YES_NO_OPTION,
109 JOptionPane.INFORMATION_MESSAGE);
110 if (result==JOptionPane.YES_OPTION) {
111 clickedNumber=0;
112 continue;//继续循环
113 }else {
114 break;// 跳出循环
115 }
116 }
117
118 } catch (InterruptedException e) {
119 e.printStackTrace();
120 }
121
122 }
123 }
124 }