回答編集履歴

1

2024/03/04 10:34

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,247 +1,124 @@
1
- `public void keyTyped`がありますが`addKeyListener`がどこにもないようです。
1
+ `public void keyTyped`がありますが`addKeyListener`がどこにもないようです。
2
-
3
- `StrArray = new String[1000]`となっていますが`words.txt`は1000行はないですよね?(十分大きめに取っているのでしょうが`String Random()`でnullが返りそうです)
2
+ `StrArray = new String[1000]`となっていますが`words.txt`は1000行はないですよね(十分大きめに取っているのでしょうが`String Random()`でnullが返りそうです)
4
-
5
-
6
-
7
3
 
8
4
 
9
5
  キーボードフォーカスではまりそうなところを追加し、今やろうとしていることを動くようにしました。
10
-
11
6
  ```Java
12
-
13
7
  package ex09;
14
8
 
15
-
16
-
17
9
  import java.awt.*;
18
-
19
10
  import java.awt.event.*;
20
-
21
11
  import java.io.File;
22
-
23
12
  import java.io.IOException;
24
-
25
13
  import java.nio.file.Files;
26
-
27
14
  import java.util.List;
28
-
29
15
  import java.util.Random;
30
-
31
16
  import javax.swing.*;
32
17
 
33
-
34
-
35
18
  public class Keytamesi2 extends JFrame implements ActionListener, KeyListener {
36
-
37
19
  private String[] strArray;
38
-
39
20
  private JLabel jl;
40
-
41
21
  private JLabel jl1;
42
-
43
22
  private JPanel pane1 = new JPanel();
44
-
45
23
  private JButton[] jb = new JButton[2];
46
-
47
24
  private int miss = 0;
48
-
49
25
  private int clearnum = 0;
50
-
51
26
  private int clear = 0;
52
-
53
27
  Random ran = new Random();
54
28
 
55
-
56
-
57
29
  public static void main(String[] args) {
58
-
59
30
  new Keytamesi2();
60
-
61
31
  }
62
32
 
63
-
64
-
65
33
  Keytamesi2() {
66
-
67
34
  // StrArray = new String[]{ "hello", "world", "java", "swing", };
68
-
69
35
  try {
70
-
71
36
  // 配列サイズを考えるのが面倒なのでリストから配列に変換
72
-
73
37
  File file = new File("C://java//words.txt");
74
-
75
38
  List<String> list = Files.readAllLines(file.toPath());
76
-
77
39
  strArray = list.toArray(new String[0]);
78
-
79
40
  } catch (IOException e) {
80
-
81
41
  e.printStackTrace();
82
-
83
42
  }
84
43
 
85
-
86
-
87
44
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
88
-
89
45
  this.setSize(500, 400);
90
-
91
46
  this.setTitle("KeyTyping2");
92
-
93
47
  setstartLabel();
94
-
95
48
  setJButton();
96
49
 
97
-
98
-
99
50
  this.addKeyListener(this);
100
-
101
51
  this.setFocusable(true);
102
-
103
52
  this.setVisible(true);
104
-
105
53
  }
106
54
 
107
-
108
-
109
55
  private void setstartLabel() {
110
-
111
56
  jl = new JLabel("タイピング");
112
-
113
57
  jl.setFont(new Font(null, Font.PLAIN, 40));
114
-
115
58
  jl.setHorizontalAlignment(JLabel.CENTER);
116
-
117
59
  jl.setForeground(Color.BLUE);
118
-
119
60
  jl.setOpaque(true);
120
-
121
61
  this.getContentPane().add(BorderLayout.CENTER, jl);
122
62
 
123
-
124
-
125
63
  jl1 = new JLabel("残り単語");
126
-
127
64
  jl1.setFont(new Font(null, Font.PLAIN, 20));
128
-
129
65
  jl1.setHorizontalAlignment(JLabel.CENTER);
130
-
131
66
  jl1.setForeground(Color.BLACK);
132
-
133
67
  this.getContentPane().add(BorderLayout.NORTH, jl1);
134
-
135
68
  }
136
69
 
137
-
138
-
139
70
  private void setJButton() {
140
-
141
71
  pane1.setLayout(new GridLayout(1, 2));
142
-
143
72
  this.getContentPane().add(BorderLayout.SOUTH, pane1);
144
73
 
145
-
146
-
147
74
  jb[0] = new JButton("easy");
148
-
149
75
  pane1.add(jb[0]);
150
-
151
76
  jb[1] = new JButton("hard");
152
-
153
77
  pane1.add(jb[1]);
154
78
 
155
-
156
-
157
79
  jb[0].addActionListener(this);
158
-
159
80
  jb[1].addActionListener(this);
160
-
161
81
  }
162
82
 
163
-
164
-
165
83
  public void actionPerformed(ActionEvent e) {
166
-
167
84
  if (e.getSource() == jb[0]) {
168
-
169
85
  clear = 5;
170
-
171
86
  } else if (e.getSource() == jb[1]) {
172
-
173
87
  clear = 10;
174
-
175
88
  }
176
89
 
177
-
178
-
179
90
  jl.setText(Random());
180
-
181
91
  requestFocusInWindow(); // フォーカスをJFrameに移しKeyEventを受けれるようにする
182
-
183
92
  }
184
93
 
185
-
186
-
187
94
  private String Random() {
188
-
189
95
  int num = ran.nextInt(strArray.length);
190
-
191
96
  return strArray[num];
192
-
193
97
  }
194
98
 
195
-
196
-
197
99
  public void keyTyped(KeyEvent e) {
198
-
199
100
  String str = jl.getText();
200
-
201
101
  System.out.println(e.getKeyChar());
202
-
203
102
  char ch = e.getKeyChar();
204
-
205
103
  if (str.charAt(0) == ch) {
206
-
207
104
  jl.setForeground(Color.BLUE);
208
-
209
105
  str = (str.substring(1));
210
106
 
211
-
212
-
213
107
  if (str.length() == 0) {
214
-
215
108
  str = Random();
216
-
217
109
  clearnum++;
218
-
219
110
  }
220
-
221
111
  if (clearnum == clear) {
222
-
223
112
  jl.setText("ミスタイプは" + miss + "回でした");
224
-
225
113
  } else {
226
-
227
114
  jl.setText(str);
228
-
229
115
  }
230
-
231
116
  } else {
232
-
233
117
  jl.setForeground(Color.RED);
234
-
235
118
  miss++;
236
-
237
119
  }
238
-
239
120
  }
240
-
241
121
  public void keyPressed(KeyEvent e) { }
242
-
243
122
  public void keyReleased(KeyEvent e) { }
244
-
245
123
  }
246
-
247
124
  ```