回答編集履歴

1

関数型インターフェースの使用

2020/08/14 05:04

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -34,6 +34,8 @@
34
34
 
35
35
  import java.awt.event.ActionListener;
36
36
 
37
+ import java.util.function.Consumer;
38
+
37
39
  import javax.swing.ImageIcon;
38
40
 
39
41
  import javax.swing.JButton;
@@ -120,7 +122,7 @@
120
122
 
121
123
  JLabel title = new JLabel("Hoge Quest");
122
124
 
123
- title.setFont(HogeQuest2.titleFont);
125
+ title.setFont(HogeQuest.titleFont);
124
126
 
125
127
  title.setForeground(Color.WHITE);
126
128
 
@@ -134,7 +136,7 @@
134
136
 
135
137
  start.setBorder(null);
136
138
 
137
- start.setFont(HogeQuest2.normalFont);
139
+ start.setFont(HogeQuest.normalFont);
138
140
 
139
141
  start.setFocusPainted(false);
140
142
 
@@ -154,7 +156,7 @@
154
156
 
155
157
  private class StartButtonHandler implements ActionListener {
156
158
 
157
- public void actionPerformed(ActionEvent event) {
159
+ @Override public void actionPerformed(ActionEvent event) {
158
160
 
159
161
  JFrame frame = (JFrame) SwingUtilities.getRoot(Title.this); // 親フレームの取得
160
162
 
@@ -190,7 +192,11 @@
190
192
 
191
193
 
192
194
 
195
+ // 関数型インターフェース 引数String 戻り値なし
196
+
197
+ // 選択肢を選んだ後の処理を記述
198
+
193
- private String position = "myHome";
199
+ private Consumer<String> choiceAction;
194
200
 
195
201
 
196
202
 
@@ -230,7 +236,7 @@
230
236
 
231
237
  textArea.setForeground(Color.WHITE);
232
238
 
233
- textArea.setFont(HogeQuest2.normalFont);
239
+ textArea.setFont(HogeQuest.normalFont);
234
240
 
235
241
  textArea.setLineWrap(true);
236
242
 
@@ -256,13 +262,13 @@
256
262
 
257
263
 
258
264
 
259
- choice1 = new JButton("本を読む");
265
+ choice1 = new JButton();
260
266
 
261
267
  choice1.setBackground(Color.BLACK);
262
268
 
263
269
  choice1.setForeground(Color.WHITE);
264
270
 
265
- choice1.setFont(HogeQuest2.normalFont);
271
+ choice1.setFont(HogeQuest.normalFont);
266
272
 
267
273
  choice1.setFocusPainted(false);
268
274
 
@@ -274,13 +280,13 @@
274
280
 
275
281
 
276
282
 
277
- choice2 = new JButton("何か食べる");
283
+ choice2 = new JButton();
278
284
 
279
285
  choice2.setBackground(Color.BLACK);
280
286
 
281
287
  choice2.setForeground(Color.WHITE);
282
288
 
283
- choice2.setFont(HogeQuest2.normalFont);
289
+ choice2.setFont(HogeQuest.normalFont);
284
290
 
285
291
  choice2.setFocusPainted(false);
286
292
 
@@ -292,13 +298,13 @@
292
298
 
293
299
 
294
300
 
295
- choice3 = new JButton("買い物に行く");
301
+ choice3 = new JButton();
296
302
 
297
303
  choice3.setBackground(Color.BLACK);
298
304
 
299
305
  choice3.setForeground(Color.WHITE);
300
306
 
301
- choice3.setFont(HogeQuest2.normalFont);
307
+ choice3.setFont(HogeQuest.normalFont);
302
308
 
303
309
  choice3.setFocusPainted(false);
304
310
 
@@ -308,14 +314,18 @@
308
314
 
309
315
  choicePanel.add(choice3);
310
316
 
317
+
318
+
319
+ myHome();
320
+
311
- }
321
+ }
322
+
323
+
312
324
 
313
325
 
314
326
 
315
327
  private void myHome() {
316
328
 
317
- position = "myHome";
318
-
319
329
  textArea.setText("");
320
330
 
321
331
  choice1.setText("本を読む");
@@ -324,14 +334,34 @@
324
334
 
325
335
  choice3.setText("買い物に行く");
326
336
 
337
+
338
+
339
+ // 匿名クラスで
340
+
341
+ choiceAction = new Consumer<String>() {
342
+
343
+ @Override public void accept(String yourChoice) {
344
+
345
+ switch (yourChoice) {
346
+
347
+ case "c1": book(); break;
348
+
349
+ case "c2": eat(); break;
350
+
351
+ case "c3": shop(); break;
352
+
353
+ }
354
+
355
+ }
356
+
357
+ };
358
+
327
359
  }
328
360
 
329
361
 
330
362
 
331
363
  private void book() {
332
364
 
333
- position = "book";
334
-
335
365
  textArea.setText("何を読もう。");
336
366
 
337
367
  choice1.setText("スッキリわかるJava入門");
@@ -340,14 +370,30 @@
340
370
 
341
371
  choice3.setText("やめる");
342
372
 
373
+
374
+
375
+ // 冗長なので以下ラムダで
376
+
377
+ choiceAction = yourChoice -> {
378
+
379
+ switch (yourChoice) {
380
+
381
+ case "c1": textArea.setText("ふむふむ。"); break;
382
+
383
+ case "c2": textArea.setText("なるほどわからん。"); break;
384
+
385
+ case "c3": myHome(); break;
386
+
387
+ }
388
+
389
+ };
390
+
343
391
  }
344
392
 
345
393
 
346
394
 
347
395
  private void eat() {
348
396
 
349
- position = "eat";
350
-
351
397
  textArea.setText("何か食べよう。");
352
398
 
353
399
  choice1.setText("ラーメン");
@@ -356,14 +402,28 @@
356
402
 
357
403
  choice3.setText("ああ、おなかいっぱい");
358
404
 
405
+
406
+
407
+ choiceAction = x -> {
408
+
409
+ switch (x) {
410
+
411
+ case "c1": textArea.setText("おいしい!"); break;
412
+
413
+ case "c2": textArea.setText("からい!"); break;
414
+
415
+ case "c3": myHome(); break;
416
+
417
+ }
418
+
419
+ };
420
+
359
421
  }
360
422
 
361
423
 
362
424
 
363
425
  private void shop() {
364
426
 
365
- position = "shop";
366
-
367
427
  imageLabel.setIcon(image2);
368
428
 
369
429
  textArea.setText("何をお買い求めになられますか?");
@@ -374,75 +434,33 @@
374
434
 
375
435
  choice3.setText("店を出る");
376
436
 
437
+
438
+
439
+ choiceAction = x -> {
440
+
441
+ switch (x) {
442
+
443
+ case "c1": textArea.setText("ラーメンですね。ありがとうございます。"); break;
444
+
445
+ case "c2": textArea.setText("カレーですね。ありがとうございます。"); break;
446
+
447
+ case "c3": myHome(); break;
448
+
449
+ }
450
+
451
+ };
452
+
377
453
  }
378
454
 
379
455
 
380
456
 
381
457
  private class ChoiceHandler implements ActionListener {
382
458
 
383
- public void actionPerformed(ActionEvent event) {
459
+ @Override public void actionPerformed(ActionEvent event) {
384
460
 
385
461
  String yourChoice = event.getActionCommand();
386
462
 
387
- switch (position) {
388
-
389
- case "myHome":
390
-
391
- switch (yourChoice) {
463
+ choiceAction.accept(yourChoice);
392
-
393
- case "c1": book(); break;
394
-
395
- case "c2": eat(); break;
396
-
397
- case "c3": shop(); break;
398
-
399
- }
400
-
401
- break;
402
-
403
- case "book":
404
-
405
- switch (yourChoice) {
406
-
407
- case "c1": textArea.setText("ふむふむ。"); break;
408
-
409
- case "c2": textArea.setText("なるほどわからん。"); break;
410
-
411
- case "c3": myHome(); break;
412
-
413
- }
414
-
415
- break;
416
-
417
- case "eat":
418
-
419
- switch (yourChoice) {
420
-
421
- case "c1": textArea.setText("おいしい!"); break;
422
-
423
- case "c2": textArea.setText("からい!"); break;
424
-
425
- case "c3": myHome(); break;
426
-
427
- }
428
-
429
- break;
430
-
431
- case "shop":
432
-
433
- switch (yourChoice) {
434
-
435
- case "c1": textArea.setText("ラーメンですね。ありがとうございます。"); break;
436
-
437
- case "c2": textArea.setText("カレーですね。ありがとうございます。"); break;
438
-
439
- case "c3": myHome(); break;
440
-
441
- }
442
-
443
- break;
444
-
445
- }
446
464
 
447
465
  }
448
466
 
@@ -461,3 +479,13 @@
461
479
  できるだけ元のレイアウトを保持したつもりですが、ずれていたらすいません。
462
480
 
463
481
  選択肢と感想?が離れたところになってしまうのがちょっと気になりますね。
482
+
483
+
484
+
485
+ ---
486
+
487
+
488
+
489
+ 追記
490
+
491
+ 関数型インターフェースを使用してちょっと近づけてみました。