質問編集履歴

2

コード訂正、全文記載

2015/07/21 02:20

投稿

fire
fire

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,18 @@
1
1
  ど初心者です。
2
2
 
3
- javaによりトランプゲームのguiを描くに当たってキーボード操作によるイベントリスナーであるキーリスナーを用いたいのですがコンパイルエラーが出ずに反応もしません。
3
+ javaによりトランプのスピードを一人で行うゲームのguiを描くに当たってキーボード操作によるイベントリスナーであるキーリスナーを用いたいのですがgui画面のみ表示され、コンパイルエラーが出ずにキー操作への反応がなされません。
4
+
4
-
5
+ keyTyped直下の操作
6
+
5
-
7
+ System.out.println("テスト");
8
+
9
+ もききません。
10
+
11
+ 稚拙な質問ですがご教授お願いいたします。
12
+
13
+
14
+
15
+ ```java
6
16
 
7
17
  import java.awt.*;
8
18
 
@@ -12,72 +22,442 @@
12
22
 
13
23
 
14
24
 
15
- public class Trump extends JPanel implements KeyListener{
16
-
17
- public Trump(){
18
-
19
- ~中略(ゲムの中身で)~
20
-
21
- }
22
-
23
-
24
-
25
- public void keyTyped(KeyEvent e){
26
-
27
- System.out.println("キータイプ済み");
28
-
29
- repaint();
30
-
31
- }
25
+ public class Speed extends JPanel implements KeyListener {
26
+
27
+
28
+
29
+ // 場、山、自陣a[4]に入れるカドを配列として用意る。
30
+
31
+ public static int[] a = new int[4], yama = new int[56], ba = new int[53];
32
+
33
+ public static int counterYAMA = 0, counterBA = 0, counterHOJU = 0;
34
+
35
+
36
+
37
+ public Speed() {
38
+
39
+
40
+
41
+ // 山に入るカードをシャッフルし配列として格納する。
42
+
43
+ int i;
44
+
45
+ for (i = 0; i < 52; i++)
46
+
47
+ yama[i] = i + 1;
48
+
49
+ for (i = 51; i > 0; i--) {
50
+
51
+ int r = (int) (Math.random() * 52) + 1;
52
+
53
+ int t = yama[r];
54
+
55
+ yama[r] = yama[i];
56
+
57
+ yama[i] = t;
58
+
59
+ }
60
+
61
+ for (i = 0; i < 4; i++)
62
+
63
+ a[i] = yama[i];
64
+
65
+ counterYAMA = 4;
66
+
67
+
68
+
69
+ yama[52]=-1;
70
+
71
+ yama[53]=-1; //この三つは最後カードがなくなったときの表示として使う。
72
+
73
+ yama[54]=-1;
74
+
75
+
76
+
77
+ // gui部分。
78
+
79
+ //アイコンに数字とスートをあてはめる。
80
+
81
+ int a0 = a[0] % 13 + 1;
82
+
83
+ int a1 = a[1] % 13 + 1;
84
+
85
+ int a2 = a[2] % 13 + 1;
86
+
87
+ int a3 = a[3] % 13 + 1;
88
+
89
+ int nba = ba[counterBA] % 13 + 1;
90
+
91
+ String suit0 = null, suit1 = null, suit2 = null, suit3 = null, suitBA = null;
92
+
93
+
94
+
95
+ if (a[0] < 14)
96
+
97
+ suit0 = "c";
98
+
99
+ else if (a[0] < 27)
100
+
101
+ suit0 = "d";
102
+
103
+ else if (a[0] < 40)
104
+
105
+ suit0 = "h";
106
+
107
+ else if (a[0] < 53)
108
+
109
+ suit0 = "s";
110
+
111
+ if (a[1] < 14)
112
+
113
+ suit1 = "c";
114
+
115
+ else if (a[1] < 27)
116
+
117
+ suit1 = "d";
118
+
119
+ else if (a[1] < 40)
120
+
121
+ suit1 = "h";
122
+
123
+ else if (a[1] < 53)
124
+
125
+ suit1 = "s";
126
+
127
+ if (a[2] < 14)
128
+
129
+ suit2 = "c";
130
+
131
+ else if (a[2] < 27)
132
+
133
+ suit2 = "d";
134
+
135
+ else if (a[2] < 40)
136
+
137
+ suit2 = "h";
138
+
139
+ else if (a[2] < 53)
140
+
141
+ suit2 = "s";
142
+
143
+ if (a[3] < 14)
144
+
145
+ suit3 = "c";
146
+
147
+ else if (a[3] < 27)
148
+
149
+ suit3 = "d";
150
+
151
+ else if (a[3] < 40)
152
+
153
+ suit3 = "h";
154
+
155
+ else if (a[3] < 53)
156
+
157
+ suit3 = "s";
158
+
159
+ if (counterBA > 0){
160
+
161
+ if (ba[counterBA] < 14)
162
+
163
+ suitBA = "c";
164
+
165
+ else if (ba[counterBA] < 27)
166
+
167
+ suitBA = "d";
168
+
169
+ else if (ba[counterBA] < 40)
170
+
171
+ suitBA = "h";
172
+
173
+ else if (ba[counterBA] < 53)
174
+
175
+ suitBA = "s";
176
+
177
+ }
178
+
179
+
180
+
181
+ Icon man1 = null,man2 = null,man3 = null,man4 = null,iba = null,iyama = null;
182
+
183
+ if (a[0] > 0)
184
+
185
+ man1 = new ImageIcon(String.format("%s%02d.gif", suit0, a0));
186
+
187
+ else
188
+
189
+ man1 = new ImageIcon("z01.gif");
190
+
191
+ if (a[1] > 0)
192
+
193
+ man2 = new ImageIcon(String.format("%s%02d.gif", suit1, a1));
194
+
195
+ else
196
+
197
+ man2 = new ImageIcon("z01.gif");
198
+
199
+ if (a[2] > 0)
200
+
201
+ man3 = new ImageIcon(String.format("%s%02d.gif", suit2, a2));
202
+
203
+ else
204
+
205
+ man3 = new ImageIcon("z01.gif");
206
+
207
+ if (a[3] > 0)
208
+
209
+ man4 = new ImageIcon(String.format("%s%02d.gif", suit3, a3));
210
+
211
+ else
212
+
213
+ man4 = new ImageIcon("z01.gif");
214
+
215
+
216
+
217
+ if (0 < counterBA && counterBA < 52)
218
+
219
+ iba = new ImageIcon(String.format("%s%02d.gif", suitBA, nba));
220
+
221
+
222
+
223
+ if (counterYAMA < 52)
224
+
225
+ iyama = new ImageIcon("z01.gif");
226
+
227
+
228
+
229
+
230
+
231
+ //パネルの配置。
232
+
233
+ JPanel p1,p2;
234
+
235
+ JButton b1, b2, b3, b4, b5, b6,b7;
236
+
237
+
238
+
239
+ p1 = new JPanel();
240
+
241
+ p2 = new JPanel();
242
+
243
+ b1 = new JButton(man1);
244
+
245
+ b2 = new JButton(man2);
246
+
247
+ b3 = new JButton(man3);
248
+
249
+ b4 = new JButton(man4);
250
+
251
+ if (counterBA == 0)
252
+
253
+ b5 = new JButton("sボタンでゲーム開始だよ!");
254
+
255
+ else if (counterBA == 52){
256
+
257
+ b5 = new JButton(String.format("finish!!\n%d回補充したね!", counterHOJU-1));
258
+
259
+ System.out.println("finish!!\n"+ (counterHOJU - 1) +"回補充したね!");
260
+
261
+ }
262
+
263
+ else
264
+
265
+ b5 = new JButton(iba);
266
+
267
+ b6 = new JButton("←場 山→");
268
+
269
+ if (counterYAMA == 52)
270
+
271
+ b7 = new JButton("ラストスパート!");
272
+
273
+ else
274
+
275
+ b7 = new JButton(iyama);
276
+
277
+
278
+
279
+ p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
280
+
281
+ p1.add(b1);
282
+
283
+ p1.add(b2);
284
+
285
+ p1.add(b3);
286
+
287
+ p1.add(b4);
288
+
289
+ p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
290
+
291
+ p2.add(b5);
292
+
293
+ p2.add(b6);
294
+
295
+ p2.add(b7);
296
+
297
+
298
+
299
+ setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
300
+
301
+ add(p2);
302
+
303
+ add(p1);
304
+
305
+
306
+
307
+ setFocusable(true);
308
+
309
+ addKeyListener(this);
310
+
311
+
312
+
313
+ // ゲーム開始!
314
+
315
+ }
316
+
317
+ public void keyTyped(KeyEvent e) {
318
+
319
+
320
+
321
+ int key = e.getKeyCode();
322
+
323
+ if (key == 's') {
324
+
325
+ System.out.println("sのキーが押された.山から場へ");
326
+
327
+ ba[counterBA] = yama[counterYAMA];
328
+
329
+ counterBA++;
330
+
331
+ counterYAMA++;
332
+
333
+ counterHOJU++;
334
+
335
+ System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
336
+
337
+ + a[2] + " " + a[3]);
338
+
339
+ } else if (key == 'h') {
340
+
341
+ System.out.println("hのキーが押された");
342
+
343
+ ba[counterBA] = a[0];
344
+
345
+ counterBA++;
346
+
347
+ if (counterYAMA < 52) {
348
+
349
+ a[0] = yama[counterYAMA];
350
+
351
+ counterYAMA++;
352
+
353
+ }
354
+
355
+ System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
356
+
357
+ + a[2] + " " + a[3]);
358
+
359
+ } else if (key == 'j') {
360
+
361
+ System.out.println("jのキーが押された");
362
+
363
+ ba[counterBA] = a[1];
364
+
365
+ counterBA++;
366
+
367
+ if (counterYAMA < 52) {
368
+
369
+ a[1] = yama[counterYAMA];
370
+
371
+ counterYAMA++;
372
+
373
+ }
374
+
375
+ System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
376
+
377
+ + a[2] + " " + a[3]);
378
+
379
+
380
+
381
+ } else if (key == 'k') {
382
+
383
+ System.out.println("kのキーが押された");
384
+
385
+ ba[counterBA] = a[2];
386
+
387
+ counterBA++;
388
+
389
+ if (counterYAMA < 52) {
390
+
391
+ a[2] = yama[counterYAMA];
392
+
393
+ counterYAMA++;
394
+
395
+ }
396
+
397
+ System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
398
+
399
+ + a[2] + " " + a[3]);
400
+
401
+ } else if (key == 'l') {
402
+
403
+ System.out.println("lのキーが押された");
404
+
405
+ ba[counterBA] = a[3];
406
+
407
+ counterBA++;
408
+
409
+ if (counterYAMA < 52) {
410
+
411
+ a[3] = yama[counterYAMA];
412
+
413
+ counterYAMA++;
414
+
415
+ }
416
+
417
+ System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
418
+
419
+ + a[2] + " " + a[3]);
420
+
421
+ }
422
+
423
+ repaint();
424
+
425
+ }
426
+
427
+
32
428
 
33
429
  public void keyPressed(KeyEvent e) {
34
430
 
35
- }
431
+ }
432
+
433
+
36
434
 
37
435
  public void keyReleased(KeyEvent e) {
38
436
 
39
- }
437
+ }
40
-
41
-
42
-
43
- public void Trumpgui(){
44
-
45
- ~中略 画面にトランプを描く操作(この画面は表示されます)~
46
-
47
- setFocusable(true);
48
-
49
- addKeyListener(this);
50
-
51
-
52
438
 
53
439
 
54
440
 
55
441
  public static void main(String[] args) {
56
442
 
57
- JFrame Trumpframe = new JFrame("JPanel");
443
+ JFrame Speedframe = new JFrame("JPanel");
444
+
58
-
445
+ Speedframe.setBackground(Color.CYAN);
446
+
59
- Trumpframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
447
+ Speedframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
60
-
448
+
61
- Trump s = new Trump();
449
+ Speed s = new Speed();
62
-
63
- Trumpgui h = new Trumpgui();
450
+
64
-
65
- Trumpframe.add(h, BorderLayout.CENTER);
451
+ Speedframe.add(s, BorderLayout.CENTER);
66
-
67
- Trumpframe.requestFocus();
452
+
68
-
69
- Trumpframe.pack();
453
+ Speedframe.pack();
70
-
454
+
71
- Trumpframe.setVisible(true);
455
+ Speedframe.setVisible(true);
72
-
73
- }
74
456
 
75
457
  }
76
458
 
77
459
 
78
460
 
79
- guiによるゲーム画面が表示されるのですがキータイプしたときの"キータイプ済み"がどうしても表示されません。
461
+ }
80
-
81
-
82
-
462
+
83
- 稚拙な質問ですがご教授お願いいたします。
463
+ ```

1

誤字の修正

2015/07/21 02:20

投稿

fire
fire

スコア13

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- public void Typed(KeyEvent e){
25
+ public void keyTyped(KeyEvent e){
26
26
 
27
27
  System.out.println("キータイプ済み");
28
28