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

回答編集履歴

3

コード修正

2017/08/17 00:04

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -73,10 +73,11 @@
73
73
  JPanel questionCard = new JPanel();
74
74
  questionCard.add(new JLabel(question.getQuestion()));
75
75
 
76
+ AnswerChecker answerChecker = new AnswerChecker(question);
76
77
  for(String answer: question.getAnswerList()) {
77
78
  JButton button = new JButton(answer);
78
79
  questionCard.add(button);
79
- button.addActionListener(new AnswerChecker(question));
80
+ button.addActionListener(answerChecker);
80
81
  }
81
82
 
82
83
  cardPanel.add(questionCard);

2

追記

2017/08/17 00:03

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -153,4 +153,6 @@
153
153
  }
154
154
  }
155
155
  }
156
- ```
156
+ ```
157
+
158
+ 構造は突貫ですし、命名はいい加減です。参考程度にお願いします。

1

追記

2017/08/16 14:27

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -5,4 +5,152 @@
5
5
  - **配列やリストを利用すること**
6
6
  それぞれの問題に対する選択肢は、`[問題数][選択肢数]`の配列で管理できます。
7
7
  配列にすることで、似たような処理を簡潔に書くことができます。
8
- また、答えも配列で管理するようにすれば、問題を追加したときも柔軟に対応できるでしょう。
8
+ また、答えも配列で管理するようにすれば、問題を追加したときも柔軟に対応できるでしょう。
9
+ (私の例では独自クラスの配列としています)
10
+
11
+ ---
12
+ 適当ですが、作ってみました。
13
+ ```Java
14
+ public class Question {
15
+ private String question;
16
+ private String[] answerList;
17
+ private int answerNum;
18
+ private String comment;
19
+
20
+ public Question(String question, String[] answerList, int answerNum, String comment) {
21
+ this.question = question;
22
+ this.answerList = answerList;
23
+ this.answerNum = answerNum;
24
+ this.comment = comment;
25
+ }
26
+
27
+ public String getQuestion() {
28
+ return question;
29
+ }
30
+ public String[] getAnswerList() {
31
+ return answerList;
32
+ }
33
+ public String getComment() {
34
+ return comment;
35
+ }
36
+
37
+ public boolean isCorrect(String answer) {
38
+ return answer.equals(answerList[answerNum]);
39
+ }
40
+ }
41
+ ```
42
+
43
+ ```Java
44
+ import javax.swing.*;
45
+ import java.awt.CardLayout;
46
+ import java.awt.BorderLayout;
47
+ import java.awt.event.*;
48
+
49
+ public class Sample extends JFrame{
50
+ private int correctedNum = 0;
51
+ private int answeredNum = 0;
52
+
53
+ private final Question[] questions = {
54
+ new Question("質問1", new String[]{"選択肢1", "選択肢2"}, 0, "解説1"),
55
+ new Question("質問2", new String[]{"選択肢3", "選択肢4"}, 1, "解説2"),
56
+ new Question("質問3", new String[]{"選択肢5", "選択肢6"}, 0, "解説3"),
57
+ };
58
+
59
+ public static void main(String[] args){
60
+ new Sample("タイトル");
61
+ }
62
+
63
+ private Sample(String title){
64
+ super(title);
65
+
66
+ //
67
+ // CardPanel
68
+ JPanel cardPanel = new JPanel();
69
+ CardLayout layout = new CardLayout();
70
+ cardPanel.setLayout(layout);
71
+
72
+ for(Question question: questions) {
73
+ JPanel questionCard = new JPanel();
74
+ questionCard.add(new JLabel(question.getQuestion()));
75
+
76
+ for(String answer: question.getAnswerList()) {
77
+ JButton button = new JButton(answer);
78
+ questionCard.add(button);
79
+ button.addActionListener(new AnswerChecker(question));
80
+ }
81
+
82
+ cardPanel.add(questionCard);
83
+ }
84
+ getContentPane().add(cardPanel, BorderLayout.CENTER);
85
+ QuestionChooser questionChooser = new QuestionChooser(layout, cardPanel);
86
+
87
+ //
88
+ // CtrlPanel
89
+ JPanel ctrlPanel = new JPanel();
90
+
91
+ JButton prevButton = new JButton("前へ");
92
+ prevButton.addActionListener(questionChooser);
93
+ prevButton.setActionCommand("previous");
94
+
95
+ JButton nextButton = new JButton("次へ");
96
+ nextButton.addActionListener(questionChooser);
97
+ nextButton.setActionCommand("next");
98
+
99
+ ctrlPanel.add(prevButton);
100
+ ctrlPanel.add(nextButton);
101
+
102
+ //
103
+ // FrameLayout
104
+ add(ctrlPanel, BorderLayout.PAGE_END);
105
+
106
+ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
107
+ setBounds(10, 10, 300, 200);
108
+ setVisible(true);
109
+ }
110
+
111
+ private class QuestionChooser implements ActionListener {
112
+ private CardLayout layout;
113
+ private JPanel panel;
114
+
115
+ QuestionChooser(CardLayout layout, JPanel panel) {
116
+ this.layout = layout;
117
+ this.panel = panel;
118
+ }
119
+
120
+ public void actionPerformed(ActionEvent e) {
121
+ String cmd = e.getActionCommand();
122
+
123
+ if( cmd.equals("next") ) {
124
+ layout.next(panel);
125
+ }
126
+ else if( cmd.equals("previous")) {
127
+ layout.previous(panel);
128
+ }
129
+ }
130
+ }
131
+
132
+ private class AnswerChecker implements ActionListener {
133
+ private Question question;
134
+
135
+ AnswerChecker(Question question) {
136
+ this.question = question;
137
+ }
138
+
139
+ public void actionPerformed(ActionEvent e) {
140
+ ++answeredNum;
141
+
142
+ String selectedAnswer = ((JButton)e.getSource()).getText();
143
+ if(question.isCorrect(selectedAnswer)) {
144
+ ++correctedNum;
145
+ }
146
+
147
+ String comment = question.getComment() + "正答数は" + correctedNum + "/" + answeredNum + "です";
148
+ showMessage(comment);
149
+ }
150
+
151
+ private void showMessage(String str) {
152
+ JOptionPane.showMessageDialog(null, new JLabel(str));
153
+ }
154
+ }
155
+ }
156
+ ```