質問編集履歴
2
コード訂正、全文記載
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -1,42 +1,232 @@
|
|
|
1
1
|
ど初心者です。
|
|
2
|
-
javaによりトランプゲームのguiを描くに当たってキーボード操作によるイベントリスナーであるキーリスナーを用いたいのですがコンパイルエラーが出ずに反応
|
|
2
|
+
javaによりトランプのスピードを一人で行うゲームのguiを描くに当たってキーボード操作によるイベントリスナーであるキーリスナーを用いたいのですがgui画面のみ表示され、コンパイルエラーが出ずにキー操作への反応がなされません。
|
|
3
|
+
keyTyped直下の操作
|
|
4
|
+
System.out.println("テスト");
|
|
5
|
+
もききません。
|
|
6
|
+
稚拙な質問ですがご教授お願いいたします。
|
|
3
7
|
|
|
8
|
+
```java
|
|
4
9
|
import java.awt.*;
|
|
5
10
|
import java.awt.event.*;
|
|
6
11
|
import javax.swing.*;
|
|
7
12
|
|
|
8
|
-
public class
|
|
13
|
+
public class Speed extends JPanel implements KeyListener {
|
|
14
|
+
|
|
15
|
+
// 場、山、自陣a[4]に入れるカードを配列として用意する。
|
|
16
|
+
public static int[] a = new int[4], yama = new int[56], ba = new int[53];
|
|
17
|
+
public static int counterYAMA = 0, counterBA = 0, counterHOJU = 0;
|
|
18
|
+
|
|
9
|
-
public
|
|
19
|
+
public Speed() {
|
|
20
|
+
|
|
21
|
+
// 山に入るカードをシャッフルし配列として格納する。
|
|
22
|
+
int i;
|
|
23
|
+
for (i = 0; i < 52; i++)
|
|
10
|
-
|
|
24
|
+
yama[i] = i + 1;
|
|
25
|
+
for (i = 51; i > 0; i--) {
|
|
26
|
+
int r = (int) (Math.random() * 52) + 1;
|
|
27
|
+
int t = yama[r];
|
|
28
|
+
yama[r] = yama[i];
|
|
29
|
+
yama[i] = t;
|
|
11
|
-
|
|
30
|
+
}
|
|
12
|
-
|
|
31
|
+
for (i = 0; i < 4; i++)
|
|
32
|
+
a[i] = yama[i];
|
|
33
|
+
counterYAMA = 4;
|
|
34
|
+
|
|
35
|
+
yama[52]=-1;
|
|
36
|
+
yama[53]=-1; //この三つは最後カードがなくなったときの表示として使う。
|
|
37
|
+
yama[54]=-1;
|
|
38
|
+
|
|
39
|
+
// gui部分。
|
|
40
|
+
//アイコンに数字とスートをあてはめる。
|
|
41
|
+
int a0 = a[0] % 13 + 1;
|
|
42
|
+
int a1 = a[1] % 13 + 1;
|
|
43
|
+
int a2 = a[2] % 13 + 1;
|
|
44
|
+
int a3 = a[3] % 13 + 1;
|
|
45
|
+
int nba = ba[counterBA] % 13 + 1;
|
|
46
|
+
String suit0 = null, suit1 = null, suit2 = null, suit3 = null, suitBA = null;
|
|
47
|
+
|
|
48
|
+
if (a[0] < 14)
|
|
49
|
+
suit0 = "c";
|
|
50
|
+
else if (a[0] < 27)
|
|
51
|
+
suit0 = "d";
|
|
52
|
+
else if (a[0] < 40)
|
|
53
|
+
suit0 = "h";
|
|
54
|
+
else if (a[0] < 53)
|
|
55
|
+
suit0 = "s";
|
|
56
|
+
if (a[1] < 14)
|
|
57
|
+
suit1 = "c";
|
|
58
|
+
else if (a[1] < 27)
|
|
59
|
+
suit1 = "d";
|
|
60
|
+
else if (a[1] < 40)
|
|
61
|
+
suit1 = "h";
|
|
62
|
+
else if (a[1] < 53)
|
|
63
|
+
suit1 = "s";
|
|
64
|
+
if (a[2] < 14)
|
|
65
|
+
suit2 = "c";
|
|
66
|
+
else if (a[2] < 27)
|
|
67
|
+
suit2 = "d";
|
|
68
|
+
else if (a[2] < 40)
|
|
69
|
+
suit2 = "h";
|
|
70
|
+
else if (a[2] < 53)
|
|
71
|
+
suit2 = "s";
|
|
72
|
+
if (a[3] < 14)
|
|
73
|
+
suit3 = "c";
|
|
74
|
+
else if (a[3] < 27)
|
|
75
|
+
suit3 = "d";
|
|
76
|
+
else if (a[3] < 40)
|
|
77
|
+
suit3 = "h";
|
|
78
|
+
else if (a[3] < 53)
|
|
79
|
+
suit3 = "s";
|
|
80
|
+
if (counterBA > 0){
|
|
81
|
+
if (ba[counterBA] < 14)
|
|
82
|
+
suitBA = "c";
|
|
83
|
+
else if (ba[counterBA] < 27)
|
|
84
|
+
suitBA = "d";
|
|
85
|
+
else if (ba[counterBA] < 40)
|
|
86
|
+
suitBA = "h";
|
|
87
|
+
else if (ba[counterBA] < 53)
|
|
88
|
+
suitBA = "s";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
Icon man1 = null,man2 = null,man3 = null,man4 = null,iba = null,iyama = null;
|
|
92
|
+
if (a[0] > 0)
|
|
93
|
+
man1 = new ImageIcon(String.format("%s%02d.gif", suit0, a0));
|
|
94
|
+
else
|
|
95
|
+
man1 = new ImageIcon("z01.gif");
|
|
96
|
+
if (a[1] > 0)
|
|
97
|
+
man2 = new ImageIcon(String.format("%s%02d.gif", suit1, a1));
|
|
98
|
+
else
|
|
99
|
+
man2 = new ImageIcon("z01.gif");
|
|
100
|
+
if (a[2] > 0)
|
|
101
|
+
man3 = new ImageIcon(String.format("%s%02d.gif", suit2, a2));
|
|
102
|
+
else
|
|
103
|
+
man3 = new ImageIcon("z01.gif");
|
|
104
|
+
if (a[3] > 0)
|
|
105
|
+
man4 = new ImageIcon(String.format("%s%02d.gif", suit3, a3));
|
|
106
|
+
else
|
|
107
|
+
man4 = new ImageIcon("z01.gif");
|
|
108
|
+
|
|
109
|
+
if (0 < counterBA && counterBA < 52)
|
|
110
|
+
iba = new ImageIcon(String.format("%s%02d.gif", suitBA, nba));
|
|
111
|
+
|
|
112
|
+
if (counterYAMA < 52)
|
|
113
|
+
iyama = new ImageIcon("z01.gif");
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
//パネルの配置。
|
|
117
|
+
JPanel p1,p2;
|
|
118
|
+
JButton b1, b2, b3, b4, b5, b6,b7;
|
|
119
|
+
|
|
120
|
+
p1 = new JPanel();
|
|
121
|
+
p2 = new JPanel();
|
|
122
|
+
b1 = new JButton(man1);
|
|
123
|
+
b2 = new JButton(man2);
|
|
124
|
+
b3 = new JButton(man3);
|
|
125
|
+
b4 = new JButton(man4);
|
|
126
|
+
if (counterBA == 0)
|
|
127
|
+
b5 = new JButton("sボタンでゲーム開始だよ!");
|
|
128
|
+
else if (counterBA == 52){
|
|
129
|
+
b5 = new JButton(String.format("finish!!\n%d回補充したね!", counterHOJU-1));
|
|
130
|
+
System.out.println("finish!!\n"+ (counterHOJU - 1) +"回補充したね!");
|
|
131
|
+
}
|
|
132
|
+
else
|
|
133
|
+
b5 = new JButton(iba);
|
|
134
|
+
b6 = new JButton("←場 山→");
|
|
135
|
+
if (counterYAMA == 52)
|
|
136
|
+
b7 = new JButton("ラストスパート!");
|
|
137
|
+
else
|
|
138
|
+
b7 = new JButton(iyama);
|
|
139
|
+
|
|
140
|
+
p1.setLayout(new BoxLayout(p1,BoxLayout.X_AXIS));
|
|
141
|
+
p1.add(b1);
|
|
142
|
+
p1.add(b2);
|
|
143
|
+
p1.add(b3);
|
|
144
|
+
p1.add(b4);
|
|
145
|
+
p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
|
|
146
|
+
p2.add(b5);
|
|
147
|
+
p2.add(b6);
|
|
148
|
+
p2.add(b7);
|
|
149
|
+
|
|
150
|
+
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
|
151
|
+
add(p2);
|
|
152
|
+
add(p1);
|
|
153
|
+
|
|
154
|
+
setFocusable(true);
|
|
155
|
+
addKeyListener(this);
|
|
156
|
+
|
|
157
|
+
// ゲーム開始!
|
|
158
|
+
}
|
|
13
|
-
|
|
159
|
+
public void keyTyped(KeyEvent e) {
|
|
160
|
+
|
|
161
|
+
int key = e.getKeyCode();
|
|
162
|
+
if (key == 's') {
|
|
163
|
+
System.out.println("sのキーが押された.山から場へ");
|
|
164
|
+
ba[counterBA] = yama[counterYAMA];
|
|
165
|
+
counterBA++;
|
|
166
|
+
counterYAMA++;
|
|
167
|
+
counterHOJU++;
|
|
168
|
+
System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
|
|
169
|
+
+ a[2] + " " + a[3]);
|
|
170
|
+
} else if (key == 'h') {
|
|
14
|
-
|
|
171
|
+
System.out.println("hのキーが押された");
|
|
172
|
+
ba[counterBA] = a[0];
|
|
173
|
+
counterBA++;
|
|
174
|
+
if (counterYAMA < 52) {
|
|
175
|
+
a[0] = yama[counterYAMA];
|
|
176
|
+
counterYAMA++;
|
|
177
|
+
}
|
|
178
|
+
System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
|
|
179
|
+
+ a[2] + " " + a[3]);
|
|
180
|
+
} else if (key == 'j') {
|
|
181
|
+
System.out.println("jのキーが押された");
|
|
182
|
+
ba[counterBA] = a[1];
|
|
183
|
+
counterBA++;
|
|
184
|
+
if (counterYAMA < 52) {
|
|
185
|
+
a[1] = yama[counterYAMA];
|
|
186
|
+
counterYAMA++;
|
|
187
|
+
}
|
|
188
|
+
System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
|
|
189
|
+
+ a[2] + " " + a[3]);
|
|
190
|
+
|
|
191
|
+
} else if (key == 'k') {
|
|
192
|
+
System.out.println("kのキーが押された");
|
|
193
|
+
ba[counterBA] = a[2];
|
|
194
|
+
counterBA++;
|
|
195
|
+
if (counterYAMA < 52) {
|
|
196
|
+
a[2] = yama[counterYAMA];
|
|
197
|
+
counterYAMA++;
|
|
198
|
+
}
|
|
199
|
+
System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
|
|
200
|
+
+ a[2] + " " + a[3]);
|
|
201
|
+
} else if (key == 'l') {
|
|
202
|
+
System.out.println("lのキーが押された");
|
|
203
|
+
ba[counterBA] = a[3];
|
|
204
|
+
counterBA++;
|
|
205
|
+
if (counterYAMA < 52) {
|
|
206
|
+
a[3] = yama[counterYAMA];
|
|
207
|
+
counterYAMA++;
|
|
208
|
+
}
|
|
209
|
+
System.out.println("場" + ba[counterBA] + " 山" + yama[counterYAMA] + "\n\n自陣" + a[0] + " " + a[1] + " "
|
|
210
|
+
+ a[2] + " " + a[3]);
|
|
211
|
+
}
|
|
15
|
-
|
|
212
|
+
repaint();
|
|
16
|
-
|
|
213
|
+
}
|
|
214
|
+
|
|
17
215
|
public void keyPressed(KeyEvent e) {
|
|
18
|
-
|
|
216
|
+
}
|
|
217
|
+
|
|
19
218
|
public void keyReleased(KeyEvent e) {
|
|
20
|
-
|
|
219
|
+
}
|
|
21
220
|
|
|
22
|
-
public void Trumpgui(){
|
|
23
|
-
~中略 画面にトランプを描く操作(この画面は表示されます)~
|
|
24
|
-
setFocusable(true);
|
|
25
|
-
addKeyListener(this);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
221
|
public static void main(String[] args) {
|
|
29
|
-
JFrame
|
|
222
|
+
JFrame Speedframe = new JFrame("JPanel");
|
|
223
|
+
Speedframe.setBackground(Color.CYAN);
|
|
30
|
-
|
|
224
|
+
Speedframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
31
|
-
|
|
225
|
+
Speed s = new Speed();
|
|
32
|
-
Trumpgui h = new Trumpgui();
|
|
33
|
-
|
|
226
|
+
Speedframe.add(s, BorderLayout.CENTER);
|
|
34
|
-
Trumpframe.requestFocus();
|
|
35
|
-
|
|
227
|
+
Speedframe.pack();
|
|
36
|
-
|
|
228
|
+
Speedframe.setVisible(true);
|
|
37
|
-
}
|
|
38
229
|
}
|
|
39
230
|
|
|
40
|
-
|
|
231
|
+
}
|
|
41
|
-
|
|
42
|
-
|
|
232
|
+
```
|
1
誤字の修正
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
~中略(ゲームの中身です)~
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
public void
|
|
13
|
+
public void keyTyped(KeyEvent e){
|
|
14
14
|
System.out.println("キータイプ済み");
|
|
15
15
|
repaint();
|
|
16
16
|
}
|