回答編集履歴
3
コード修正
test
CHANGED
@@ -148,13 +148,15 @@
|
|
148
148
|
|
149
149
|
|
150
150
|
|
151
|
+
AnswerChecker answerChecker = new AnswerChecker(question);
|
152
|
+
|
151
153
|
for(String answer: question.getAnswerList()) {
|
152
154
|
|
153
155
|
JButton button = new JButton(answer);
|
154
156
|
|
155
157
|
questionCard.add(button);
|
156
158
|
|
157
|
-
button.addActionListener(n
|
159
|
+
button.addActionListener(answerChecker);
|
158
160
|
|
159
161
|
}
|
160
162
|
|
2
追記
test
CHANGED
@@ -309,3 +309,7 @@
|
|
309
309
|
}
|
310
310
|
|
311
311
|
```
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
構造は突貫ですし、命名はいい加減です。参考程度にお願いします。
|
1
追記
test
CHANGED
@@ -13,3 +13,299 @@
|
|
13
13
|
配列にすることで、似たような処理を簡潔に書くことができます。
|
14
14
|
|
15
15
|
また、答えも配列で管理するようにすれば、問題を追加したときも柔軟に対応できるでしょう。
|
16
|
+
|
17
|
+
(私の例では独自クラスの配列としています)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
適当ですが、作ってみました。
|
24
|
+
|
25
|
+
```Java
|
26
|
+
|
27
|
+
public class Question {
|
28
|
+
|
29
|
+
private String question;
|
30
|
+
|
31
|
+
private String[] answerList;
|
32
|
+
|
33
|
+
private int answerNum;
|
34
|
+
|
35
|
+
private String comment;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
public Question(String question, String[] answerList, int answerNum, String comment) {
|
40
|
+
|
41
|
+
this.question = question;
|
42
|
+
|
43
|
+
this.answerList = answerList;
|
44
|
+
|
45
|
+
this.answerNum = answerNum;
|
46
|
+
|
47
|
+
this.comment = comment;
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public String getQuestion() {
|
54
|
+
|
55
|
+
return question;
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
public String[] getAnswerList() {
|
60
|
+
|
61
|
+
return answerList;
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
public String getComment() {
|
66
|
+
|
67
|
+
return comment;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
public boolean isCorrect(String answer) {
|
74
|
+
|
75
|
+
return answer.equals(answerList[answerNum]);
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
```Java
|
86
|
+
|
87
|
+
import javax.swing.*;
|
88
|
+
|
89
|
+
import java.awt.CardLayout;
|
90
|
+
|
91
|
+
import java.awt.BorderLayout;
|
92
|
+
|
93
|
+
import java.awt.event.*;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
public class Sample extends JFrame{
|
98
|
+
|
99
|
+
private int correctedNum = 0;
|
100
|
+
|
101
|
+
private int answeredNum = 0;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
private final Question[] questions = {
|
106
|
+
|
107
|
+
new Question("質問1", new String[]{"選択肢1", "選択肢2"}, 0, "解説1"),
|
108
|
+
|
109
|
+
new Question("質問2", new String[]{"選択肢3", "選択肢4"}, 1, "解説2"),
|
110
|
+
|
111
|
+
new Question("質問3", new String[]{"選択肢5", "選択肢6"}, 0, "解説3"),
|
112
|
+
|
113
|
+
};
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
public static void main(String[] args){
|
118
|
+
|
119
|
+
new Sample("タイトル");
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
private Sample(String title){
|
126
|
+
|
127
|
+
super(title);
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
//
|
132
|
+
|
133
|
+
// CardPanel
|
134
|
+
|
135
|
+
JPanel cardPanel = new JPanel();
|
136
|
+
|
137
|
+
CardLayout layout = new CardLayout();
|
138
|
+
|
139
|
+
cardPanel.setLayout(layout);
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
for(Question question: questions) {
|
144
|
+
|
145
|
+
JPanel questionCard = new JPanel();
|
146
|
+
|
147
|
+
questionCard.add(new JLabel(question.getQuestion()));
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
for(String answer: question.getAnswerList()) {
|
152
|
+
|
153
|
+
JButton button = new JButton(answer);
|
154
|
+
|
155
|
+
questionCard.add(button);
|
156
|
+
|
157
|
+
button.addActionListener(new AnswerChecker(question));
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
cardPanel.add(questionCard);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
getContentPane().add(cardPanel, BorderLayout.CENTER);
|
168
|
+
|
169
|
+
QuestionChooser questionChooser = new QuestionChooser(layout, cardPanel);
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
//
|
174
|
+
|
175
|
+
// CtrlPanel
|
176
|
+
|
177
|
+
JPanel ctrlPanel = new JPanel();
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
JButton prevButton = new JButton("前へ");
|
182
|
+
|
183
|
+
prevButton.addActionListener(questionChooser);
|
184
|
+
|
185
|
+
prevButton.setActionCommand("previous");
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
JButton nextButton = new JButton("次へ");
|
190
|
+
|
191
|
+
nextButton.addActionListener(questionChooser);
|
192
|
+
|
193
|
+
nextButton.setActionCommand("next");
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
ctrlPanel.add(prevButton);
|
198
|
+
|
199
|
+
ctrlPanel.add(nextButton);
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
//
|
204
|
+
|
205
|
+
// FrameLayout
|
206
|
+
|
207
|
+
add(ctrlPanel, BorderLayout.PAGE_END);
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
212
|
+
|
213
|
+
setBounds(10, 10, 300, 200);
|
214
|
+
|
215
|
+
setVisible(true);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
private class QuestionChooser implements ActionListener {
|
222
|
+
|
223
|
+
private CardLayout layout;
|
224
|
+
|
225
|
+
private JPanel panel;
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
QuestionChooser(CardLayout layout, JPanel panel) {
|
230
|
+
|
231
|
+
this.layout = layout;
|
232
|
+
|
233
|
+
this.panel = panel;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
public void actionPerformed(ActionEvent e) {
|
240
|
+
|
241
|
+
String cmd = e.getActionCommand();
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
if( cmd.equals("next") ) {
|
246
|
+
|
247
|
+
layout.next(panel);
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
else if( cmd.equals("previous")) {
|
252
|
+
|
253
|
+
layout.previous(panel);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
private class AnswerChecker implements ActionListener {
|
264
|
+
|
265
|
+
private Question question;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
AnswerChecker(Question question) {
|
270
|
+
|
271
|
+
this.question = question;
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
public void actionPerformed(ActionEvent e) {
|
278
|
+
|
279
|
+
++answeredNum;
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
String selectedAnswer = ((JButton)e.getSource()).getText();
|
284
|
+
|
285
|
+
if(question.isCorrect(selectedAnswer)) {
|
286
|
+
|
287
|
+
++correctedNum;
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
String comment = question.getComment() + "正答数は" + correctedNum + "/" + answeredNum + "です";
|
294
|
+
|
295
|
+
showMessage(comment);
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
private void showMessage(String str) {
|
302
|
+
|
303
|
+
JOptionPane.showMessageDialog(null, new JLabel(str));
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
```
|