回答編集履歴

3

回答ボタンをパネルに統合(AnswerPanel), クイズ毎に選択肢数が変えられるようになった.

2019/09/10 09:54

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  import java.awt.Dimension;
16
16
 
17
+ import java.awt.event.ActionEvent;
18
+
17
19
  import java.awt.event.ActionListener;
18
20
 
19
21
  import java.awt.event.WindowEvent;
@@ -94,7 +96,7 @@
94
96
 
95
97
  new Quiz("ドラえもんに登場するスネ夫の苗字は?",
96
98
 
97
- new String[]{"骨川", "馬川"}),
99
+ new String[]{"骨川", "馬川", "スネ山"}),
98
100
 
99
101
  new Quiz("ドラえもんの好物はどら焼き、では妹のドラミちゃんの好物は?",
100
102
 
@@ -122,11 +124,9 @@
122
124
 
123
125
 
124
126
 
125
- static private final int ANSWERS_SIZE = 2;
126
-
127
127
  private JLabel questionLabel;
128
128
 
129
- private JButton[] answerButtons = new JButton[ANSWERS_SIZE];
129
+ private AnswerPanel answerPanel;
130
130
 
131
131
  private SouthPanel southPanel;
132
132
 
@@ -164,21 +164,7 @@
164
164
 
165
165
 
166
166
 
167
- JPanel answerPanel = new JPanel();
168
-
169
- for(int i=0; i<answerButtons.length; i++) {
170
-
171
- answerButtons[i] = new JButton();
172
-
173
- answerButtons[i].addActionListener(e -> {
174
-
175
- decide(((JButton)e.getSource()).getText());
176
-
177
- });
178
-
179
- answerPanel.add(answerButtons[i]);
167
+ answerPanel = new AnswerPanel(answer->decide(answer));
180
-
181
- }
182
168
 
183
169
 
184
170
 
@@ -204,6 +190,60 @@
204
190
 
205
191
 
206
192
 
193
+ //回答ボタン群
194
+
195
+ static private class AnswerPanel extends JPanel {
196
+
197
+ interface AnswerListener {
198
+
199
+ void decide(String answer);
200
+
201
+ }
202
+
203
+ private AnswerListener answerListener;
204
+
205
+ private final ActionListener actionListener = new ActionListener() {
206
+
207
+ @Override
208
+
209
+ public void actionPerformed(ActionEvent e) {
210
+
211
+ String answer = ((JButton)e.getSource()).getText();
212
+
213
+ answerListener.decide(answer);
214
+
215
+ }
216
+
217
+ };
218
+
219
+ AnswerPanel(AnswerListener answerListener) {
220
+
221
+ this.answerListener = answerListener;
222
+
223
+ }
224
+
225
+ //回答数に応じてボタン配置
226
+
227
+ void setAnswers(String[] answers) {
228
+
229
+ removeAll();
230
+
231
+ for(int i=0; i<answers.length; i++) {
232
+
233
+ JButton button = new JButton(answers[i]);
234
+
235
+ button.addActionListener(actionListener);
236
+
237
+ add(button);
238
+
239
+ }
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
207
247
  //下部の表示を進行状況に応じて切り替える
208
248
 
209
249
  static private class SouthPanel extends JPanel {
@@ -290,11 +330,7 @@
290
330
 
291
331
  questionLabel.setForeground(Color.red);
292
332
 
293
- for(int i=0; i<answerButtons.length; i++) {
294
-
295
- answerButtons[i].setText(quiz.answers[i]);
333
+ answerPanel.setAnswers(quiz.answers);
296
-
297
- }
298
334
 
299
335
  }
300
336
 

2

反映が中途でした><

2019/09/10 09:54

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -132,9 +132,9 @@
132
132
 
133
133
 
134
134
 
135
- public QuizGame(String title,int width,int height) {
135
+ public QuizGame(String title, int width, int height) {
136
-
136
+
137
- super();
137
+ super(title);
138
138
 
139
139
  setVisible(true);
140
140
 

1

GAME クラスを反映

2019/09/10 09:01

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -46,9 +46,7 @@
46
46
 
47
47
  public static void main(String[] args){
48
48
 
49
- QuizGame gm = new QuizGame();
49
+ new QuizGame("NOBUクイズ", 500, 500);
50
-
51
- gm.setVisible(true);
52
50
 
53
51
  }
54
52
 
@@ -134,13 +132,17 @@
134
132
 
135
133
 
136
134
 
135
+ public QuizGame(String title,int width,int height) {
136
+
137
+ super();
138
+
137
- public QuizGame() {
139
+ setVisible(true);
140
+
138
-
141
+ setSize(width, height);
142
+
139
- setDefaultCloseOperation(EXIT_ON_CLOSE);
143
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140
-
141
- setTitle("NOBUクイズ");
144
+
142
-
143
- setSize(500, 500);
145
+ setResizable(false);
144
146
 
145
147
 
146
148
 
@@ -170,7 +172,7 @@
170
172
 
171
173
  answerButtons[i].addActionListener(e -> {
172
174
 
173
- decide(((JButton)(e.getSource())).getText());
175
+ decide(((JButton)e.getSource()).getText());
174
176
 
175
177
  });
176
178