回答編集履歴
2
コードに文章追加
test
CHANGED
@@ -6,6 +6,10 @@
|
|
6
6
|
|
7
7
|
---
|
8
8
|
|
9
|
+
時間を計り終わった後の処理("再実行"ボタンや"戻る"ボタン?)が無かったり, ボタン連打が出来たりと不足部分はありますが, 一応動作します.
|
10
|
+
|
11
|
+
MainFrame.java
|
12
|
+
|
9
13
|
```java
|
10
14
|
|
11
15
|
import java.awt.*;
|
@@ -80,7 +84,7 @@
|
|
80
84
|
|
81
85
|
layout.show(panel, "ClickPanel");
|
82
86
|
|
83
|
-
clickPanel.go(mode);
|
87
|
+
clickPanel.go(mode);
|
84
88
|
|
85
89
|
}
|
86
90
|
|
1
コード追加
test
CHANGED
@@ -1,3 +1,301 @@
|
|
1
1
|
Swing のイベント処理はシングルスレッドです.
|
2
2
|
|
3
3
|
actionPerformed メソッドが終わらない限り, actionPerformed メソッド内で実行した Swing に関する表示等は行われません.
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
```java
|
10
|
+
|
11
|
+
import java.awt.*;
|
12
|
+
|
13
|
+
import java.awt.event.*;
|
14
|
+
|
15
|
+
import java.util.Random;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
import javax.swing.*;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
public class MainFrame extends JFrame {
|
24
|
+
|
25
|
+
public MainFrame() {
|
26
|
+
|
27
|
+
super("選択");
|
28
|
+
|
29
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
30
|
+
|
31
|
+
setSize(600,600);
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
Container contentPane= getContentPane();
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
JLabel label = new JLabel("");
|
40
|
+
|
41
|
+
//label.setBackground( Color.orange );
|
42
|
+
|
43
|
+
contentPane.add(label, BorderLayout.NORTH);
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
CardLayout layout = new CardLayout();
|
48
|
+
|
49
|
+
JPanel panel = new JPanel(layout);
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
ChoisePanel choisePanel = new ChoisePanel();
|
54
|
+
|
55
|
+
panel.add(choisePanel, "ChoisePanel");
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
ClickPanel clickPanel = new ClickPanel();
|
60
|
+
|
61
|
+
panel.add(clickPanel, "ClickPanel");
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
contentPane.add(panel,BorderLayout.CENTER);
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
choisePanel.setOnChoiseListener(new ChoisePanel.OnChoiseListener() {
|
70
|
+
|
71
|
+
@Override
|
72
|
+
|
73
|
+
public void onChoise(int no) {
|
74
|
+
|
75
|
+
ClickPanel.TextMode mode = no == 1 ? ClickPanel.TextMode.ON : ClickPanel.TextMode.OFF;
|
76
|
+
|
77
|
+
setTitle("早押し速度計測");
|
78
|
+
|
79
|
+
label.setText("押せと言ったらクリックせよ");
|
80
|
+
|
81
|
+
layout.show(panel, "ClickPanel");
|
82
|
+
|
83
|
+
clickPanel.go(mode);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
});
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
label.setText("どちらのモード?");
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
static class ChoisePanel extends JPanel {
|
98
|
+
|
99
|
+
interface OnChoiseListener {
|
100
|
+
|
101
|
+
/**
|
102
|
+
|
103
|
+
* @param no 1 or 2.
|
104
|
+
|
105
|
+
*/
|
106
|
+
|
107
|
+
void onChoise(int no);
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
private OnChoiseListener listener;
|
112
|
+
|
113
|
+
ChoisePanel() {
|
114
|
+
|
115
|
+
super(null);
|
116
|
+
|
117
|
+
setBackground(Color.orange);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
JTextArea textArea = new JTextArea();
|
122
|
+
|
123
|
+
textArea.setBounds(100,500,400,30);
|
124
|
+
|
125
|
+
textArea.setText("クリックで選択せよ");
|
126
|
+
|
127
|
+
add(textArea);
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
//ボタンの生成
|
132
|
+
|
133
|
+
JButton button1 = new JButton("文字表記あり 音あり");
|
134
|
+
|
135
|
+
button1.setBounds(70,180,200,200);
|
136
|
+
|
137
|
+
button1.addActionListener(new ActionListener() {
|
138
|
+
|
139
|
+
@Override
|
140
|
+
|
141
|
+
public void actionPerformed(ActionEvent e) {
|
142
|
+
|
143
|
+
if(listener != null) listener.onChoise(1);
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
});
|
148
|
+
|
149
|
+
add(button1);
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
JButton button2 = new JButton("文字表記なし 音のみ");
|
154
|
+
|
155
|
+
button2.setBounds(280,180,200,200);
|
156
|
+
|
157
|
+
button2.addActionListener(new ActionListener() {
|
158
|
+
|
159
|
+
@Override
|
160
|
+
|
161
|
+
public void actionPerformed(ActionEvent e) {
|
162
|
+
|
163
|
+
if(listener != null) listener.onChoise(2);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
});
|
168
|
+
|
169
|
+
add(button2);
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
void setOnChoiseListener(OnChoiseListener listener) {
|
174
|
+
|
175
|
+
this.listener = listener;
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
static class ClickPanel extends JPanel {
|
184
|
+
|
185
|
+
enum TextMode {
|
186
|
+
|
187
|
+
ON, OFF
|
188
|
+
|
189
|
+
};
|
190
|
+
|
191
|
+
private long start_point;
|
192
|
+
|
193
|
+
private JTextArea textArea;
|
194
|
+
|
195
|
+
private boolean flag;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public ClickPanel() {
|
200
|
+
|
201
|
+
super(null);
|
202
|
+
|
203
|
+
setBackground(Color.orange);
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
textArea = new JTextArea();
|
208
|
+
|
209
|
+
textArea.setBounds(100,500,400,30);
|
210
|
+
|
211
|
+
add(textArea);
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//ボタンの生成
|
216
|
+
|
217
|
+
JButton button = new JButton(new ImageIcon("ose.jpg"));
|
218
|
+
|
219
|
+
button.setBounds(140,180,290,170);
|
220
|
+
|
221
|
+
button.addActionListener(new ActionListener() {
|
222
|
+
|
223
|
+
@Override
|
224
|
+
|
225
|
+
public void actionPerformed(ActionEvent e){
|
226
|
+
|
227
|
+
if(!flag) return;
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
long end_point = System.currentTimeMillis();
|
232
|
+
|
233
|
+
System.out.println("終了時間" + end_point + "ms");
|
234
|
+
|
235
|
+
System.out.println("time: " + (end_point - start_point) + "ms");
|
236
|
+
|
237
|
+
long time = end_point - start_point;
|
238
|
+
|
239
|
+
System.out.println(time);
|
240
|
+
|
241
|
+
textArea.setText("Your Time is " + time + "ms");
|
242
|
+
|
243
|
+
flag = false;
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
});
|
248
|
+
|
249
|
+
add(button);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
void go(TextMode mode) {
|
256
|
+
|
257
|
+
flag = false;
|
258
|
+
|
259
|
+
Random random = new Random(System.currentTimeMillis());
|
260
|
+
|
261
|
+
int delay = (random.nextInt(5)+2)*1000;
|
262
|
+
|
263
|
+
Timer timer = new Timer(delay, new ActionListener() {
|
264
|
+
|
265
|
+
@Override
|
266
|
+
|
267
|
+
public void actionPerformed(ActionEvent e) {
|
268
|
+
|
269
|
+
if(mode == TextMode.ON) textArea.setText(" 押せ! ");
|
270
|
+
|
271
|
+
java.awt.Toolkit.getDefaultToolkit().beep();
|
272
|
+
|
273
|
+
start_point = System.currentTimeMillis();
|
274
|
+
|
275
|
+
System.out.println("開始時間" + start_point + "ms");
|
276
|
+
|
277
|
+
flag = true;
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
});
|
282
|
+
|
283
|
+
timer.setRepeats(false);
|
284
|
+
|
285
|
+
timer.start();
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
public static void main(String[] args) {
|
294
|
+
|
295
|
+
new MainFrame().setVisible(true);
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
```
|