1 import java.util.Random;
2 import java.util.Timer;
3 import java.util.TimerTask;
4
5 import org.eclipse.swt.graphics.Image;
6 import org.eclipse.swt.layout.GridData;
7 import org.eclipse.swt.widgets.Display;
8 import org.eclipse.swt.widgets.Label;
9 import org.eclipse.swt.widgets.Shell;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.MouseEvent;
13 import org.eclipse.swt.events.MouseListener;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17
18 public class PlayMouse {
19 public static int sorce = 0;
20 private Display display = Display.getDefault();;
21 private Shell shell = new Shell(display);
22 public Label[] labels = { new Label(shell, SWT.NONE), new Label(shell, SWT.NONE), new Label(shell, SWT.NONE),
23 new Label(shell, SWT.NONE), new Label(shell, SWT.NONE), new Label(shell, SWT.NONE),
24 new Label(shell, SWT.NONE), new Label(shell, SWT.NONE), new Label(shell, SWT.NONE) };
25 // private Thread thread = null;
26 private Runnable runnable;
27 private boolean exit = true;
28 public static void main(String[] args) {
29 PlayMouse playMouse = new PlayMouse();
30
31 }
32
33 public PlayMouse() {
34
35 shell.setSize(800, 600);
36 shell.setText("PlayMouse");
37 shell.setLayout(null);
38
39 shell.setBackgroundImage(new Image(display, "Image\\3.jpg"));
40 Image image = new Image(display, "Image\\11.jpg");
41
42 labels[0].setBounds(38, 147, 111, 74);
43 labels[1].setBounds(291, 147, 111, 74);
44 labels[2].setBounds(603, 147, 111, 74);
45 labels[3].setBounds(38, 318, 111, 74);
46 labels[4].setBounds(291, 318, 111, 74);
47 labels[5].setBounds(603, 318, 111, 74);
48 labels[6].setBounds(38, 445, 111, 74);
49 labels[7].setBounds(291, 445, 111, 74);
50 labels[8].setBounds(603, 445, 111, 74);
51
52 Label label = new Label(shell, SWT.NONE);
53 label.setBounds(539, 29, 123, 20);
54 label.setText("您的分数为:" + sorce + " 分");
55
56 Button btnNewButton = new Button(shell, SWT.NONE);
57 btnNewButton.setBounds(0, 44, 98, 30);
58 btnNewButton.setText("开始");
59 Button btnNewButton_1 = new Button(shell, SWT.NONE);
60 btnNewButton_1.setBounds(126, 44, 98, 30);
61 btnNewButton_1.setText("停止");
62 btnNewButton_1.setEnabled(false);
63 btnNewButton.addSelectionListener(new SelectionAdapter() {
64 @Override
65 public void widgetSelected(SelectionEvent e) {
66 Thread thread = null;
67 display.asyncExec(thread);
68 thread = new Thread() {
69 public void run() {
70 exit = true;//用于停止时跳出循环
71 while (exit) {
72 display.asyncExec(runnable = new Runnable() {
73 @Override
74 public void run() {
75 for (Label l : labels) {
76 l.setVisible(false);
77 }
78 int mousenum = (int) (Math.random() * 9);
79 System.out.println(mousenum);
80 labels[mousenum].setVisible(true);
81 }
82 });
83 try {
84 thread.sleep(1000);
85 } catch (InterruptedException e) {
86 // TODO Auto-generated catch block
87 e.printStackTrace();
88 }
89 }
90 }
91 };
92 thread.start();
93 btnNewButton.setEnabled(!btnNewButton.isEnabled());
94 btnNewButton_1.setEnabled(!btnNewButton_1.isEnabled());
95 }
96 });
97
98 btnNewButton_1.addSelectionListener(new SelectionAdapter() {
99 @Override
100 public void widgetSelected(SelectionEvent e) {
101 exit = false;//跳出循环线程终止
102 btnNewButton.setEnabled(!btnNewButton.isEnabled());
103 btnNewButton_1.setEnabled(!btnNewButton_1.isEnabled());
104 }
105 });
106
107 for (Label l : labels) {
108 l.setText("");
109 l.setBackgroundImage(image);
110 // l.setVisible(false);
111 l.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
112 @Override
113 public void mouseDown(MouseEvent e) {
114 PlayMouse.sorce++;
115 label.setText("您的分数为:" + sorce + " 分");
116 l.setVisible(false);
117 }
118 });
119 }
120 shell.open();
121 shell.layout();
122 while (!shell.isDisposed()) {
123 if (!display.readAndDispatch()) {
124 display.sleep();
125 }
126 }
127
128 }
129 }