質問するログイン新規登録

回答編集履歴

1

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

2020/08/14 05:04

投稿

TN8001
TN8001

スコア10180

answer CHANGED
@@ -16,6 +16,7 @@
16
16
  import java.awt.GridLayout;
17
17
  import java.awt.event.ActionEvent;
18
18
  import java.awt.event.ActionListener;
19
+ import java.util.function.Consumer;
19
20
  import javax.swing.ImageIcon;
20
21
  import javax.swing.JButton;
21
22
  import javax.swing.JFrame;
@@ -59,14 +60,14 @@
59
60
  setBackground(Color.BLACK);
60
61
 
61
62
  JLabel title = new JLabel("Hoge Quest");
62
- title.setFont(HogeQuest2.titleFont);
63
+ title.setFont(HogeQuest.titleFont);
63
64
  title.setForeground(Color.WHITE);
64
65
  title.setBounds(380, 270, 500, 120);
65
66
  add(title);
66
67
 
67
68
  JButton start = new JButton("Start");
68
69
  start.setBorder(null);
69
- start.setFont(HogeQuest2.normalFont);
70
+ start.setFont(HogeQuest.normalFont);
70
71
  start.setFocusPainted(false);
71
72
  start.setBackground(Color.BLACK);
72
73
  start.setForeground(Color.WHITE);
@@ -76,7 +77,7 @@
76
77
  }
77
78
 
78
79
  private class StartButtonHandler implements ActionListener {
79
- public void actionPerformed(ActionEvent event) {
80
+ @Override public void actionPerformed(ActionEvent event) {
80
81
  JFrame frame = (JFrame) SwingUtilities.getRoot(Title.this); // 親フレームの取得
81
82
  Container contentPane = frame.getContentPane();
82
83
  CardLayout layout = (CardLayout) contentPane.getLayout(); // レイアウトの取得
@@ -94,7 +95,9 @@
94
95
  private final ImageIcon image1 = new ImageIcon("./image01.jpg");
95
96
  private final ImageIcon image2 = new ImageIcon("./image02.jpg");
96
97
 
98
+ // 関数型インターフェース 引数String 戻り値なし
99
+ // 選択肢を選んだ後の処理を記述
97
- private String position = "myHome";
100
+ private Consumer<String> choiceAction;
98
101
 
99
102
  public Home() {
100
103
  setLayout(null);
@@ -114,7 +117,7 @@
114
117
  textArea.setBounds(60, 520, 680, 200);
115
118
  textArea.setBackground(Color.BLACK);
116
119
  textArea.setForeground(Color.WHITE);
117
- textArea.setFont(HogeQuest2.normalFont);
120
+ textArea.setFont(HogeQuest.normalFont);
118
121
  textArea.setLineWrap(true);
119
122
  textPanel.add(textArea);
120
123
  add(textPanel);
@@ -127,100 +130,106 @@
127
130
 
128
131
  ChoiceHandler choiceHandler = new ChoiceHandler();
129
132
 
130
- choice1 = new JButton("本を読む");
133
+ choice1 = new JButton();
131
134
  choice1.setBackground(Color.BLACK);
132
135
  choice1.setForeground(Color.WHITE);
133
- choice1.setFont(HogeQuest2.normalFont);
136
+ choice1.setFont(HogeQuest.normalFont);
134
137
  choice1.setFocusPainted(false);
135
138
  choice1.addActionListener(choiceHandler);
136
139
  choice1.setActionCommand("c1");
137
140
  choicePanel.add(choice1);
138
141
 
139
- choice2 = new JButton("何か食べる");
142
+ choice2 = new JButton();
140
143
  choice2.setBackground(Color.BLACK);
141
144
  choice2.setForeground(Color.WHITE);
142
- choice2.setFont(HogeQuest2.normalFont);
145
+ choice2.setFont(HogeQuest.normalFont);
143
146
  choice2.setFocusPainted(false);
144
147
  choice2.addActionListener(choiceHandler);
145
148
  choice2.setActionCommand("c2");
146
149
  choicePanel.add(choice2);
147
150
 
148
- choice3 = new JButton("買い物に行く");
151
+ choice3 = new JButton();
149
152
  choice3.setBackground(Color.BLACK);
150
153
  choice3.setForeground(Color.WHITE);
151
- choice3.setFont(HogeQuest2.normalFont);
154
+ choice3.setFont(HogeQuest.normalFont);
152
155
  choice3.setFocusPainted(false);
153
156
  choice3.addActionListener(choiceHandler);
154
157
  choice3.setActionCommand("c3");
155
158
  choicePanel.add(choice3);
159
+
160
+ myHome();
156
161
  }
157
162
 
163
+
158
164
  private void myHome() {
159
- position = "myHome";
160
165
  textArea.setText("");
161
166
  choice1.setText("本を読む");
162
167
  choice2.setText("何か食べる");
163
168
  choice3.setText("買い物に行く");
169
+
170
+ // 匿名クラスで
171
+ choiceAction = new Consumer<String>() {
172
+ @Override public void accept(String yourChoice) {
173
+ switch (yourChoice) {
174
+ case "c1": book(); break;
175
+ case "c2": eat(); break;
176
+ case "c3": shop(); break;
177
+ }
178
+ }
179
+ };
164
180
  }
165
181
 
166
182
  private void book() {
167
- position = "book";
168
183
  textArea.setText("何を読もう。");
169
184
  choice1.setText("スッキリわかるJava入門");
170
185
  choice2.setText("オブジェクト指向でなぜつくるのか");
171
186
  choice3.setText("やめる");
187
+
188
+ // 冗長なので以下ラムダで
189
+ choiceAction = yourChoice -> {
190
+ switch (yourChoice) {
191
+ case "c1": textArea.setText("ふむふむ。"); break;
192
+ case "c2": textArea.setText("なるほどわからん。"); break;
193
+ case "c3": myHome(); break;
194
+ }
195
+ };
172
196
  }
173
197
 
174
198
  private void eat() {
175
- position = "eat";
176
199
  textArea.setText("何か食べよう。");
177
200
  choice1.setText("ラーメン");
178
201
  choice2.setText("カレーライス");
179
202
  choice3.setText("ああ、おなかいっぱい");
203
+
204
+ choiceAction = x -> {
205
+ switch (x) {
206
+ case "c1": textArea.setText("おいしい!"); break;
207
+ case "c2": textArea.setText("からい!"); break;
208
+ case "c3": myHome(); break;
209
+ }
210
+ };
180
211
  }
181
212
 
182
213
  private void shop() {
183
- position = "shop";
184
214
  imageLabel.setIcon(image2);
185
215
  textArea.setText("何をお買い求めになられますか?");
186
216
  choice1.setText("ラーメン");
187
217
  choice2.setText("カレーライス");
188
218
  choice3.setText("店を出る");
219
+
220
+ choiceAction = x -> {
221
+ switch (x) {
222
+ case "c1": textArea.setText("ラーメンですね。ありがとうございます。"); break;
223
+ case "c2": textArea.setText("カレーですね。ありがとうございます。"); break;
224
+ case "c3": myHome(); break;
225
+ }
226
+ };
189
227
  }
190
228
 
191
229
  private class ChoiceHandler implements ActionListener {
192
- public void actionPerformed(ActionEvent event) {
230
+ @Override public void actionPerformed(ActionEvent event) {
193
231
  String yourChoice = event.getActionCommand();
194
- switch (position) {
195
- case "myHome":
196
- switch (yourChoice) {
232
+ choiceAction.accept(yourChoice);
197
- case "c1": book(); break;
198
- case "c2": eat(); break;
199
- case "c3": shop(); break;
200
- }
201
- break;
202
- case "book":
203
- switch (yourChoice) {
204
- case "c1": textArea.setText("ふむふむ。"); break;
205
- case "c2": textArea.setText("なるほどわからん。"); break;
206
- case "c3": myHome(); break;
207
- }
208
- break;
209
- case "eat":
210
- switch (yourChoice) {
211
- case "c1": textArea.setText("おいしい!"); break;
212
- case "c2": textArea.setText("からい!"); break;
213
- case "c3": myHome(); break;
214
- }
215
- break;
216
- case "shop":
217
- switch (yourChoice) {
218
- case "c1": textArea.setText("ラーメンですね。ありがとうございます。"); break;
219
- case "c2": textArea.setText("カレーですね。ありがとうございます。"); break;
220
- case "c3": myHome(); break;
221
- }
222
- break;
223
- }
224
233
  }
225
234
  }
226
235
  }
@@ -229,4 +238,9 @@
229
238
  ---
230
239
 
231
240
  できるだけ元のレイアウトを保持したつもりですが、ずれていたらすいません。
232
- 選択肢と感想?が離れたところになってしまうのがちょっと気になりますね。
241
+ 選択肢と感想?が離れたところになってしまうのがちょっと気になりますね。
242
+
243
+ ---
244
+
245
+ 追記
246
+ 関数型インターフェースを使用してちょっと近づけてみました。