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

回答編集履歴

3

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

2019/09/10 09:54

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -6,6 +6,7 @@
6
6
  import java.awt.CardLayout;
7
7
  import java.awt.Color;
8
8
  import java.awt.Dimension;
9
+ import java.awt.event.ActionEvent;
9
10
  import java.awt.event.ActionListener;
10
11
  import java.awt.event.WindowEvent;
11
12
  import java.util.ArrayDeque;
@@ -46,7 +47,7 @@
46
47
 
47
48
  static private Quiz[] quizArray = {
48
49
  new Quiz("ドラえもんに登場するスネ夫の苗字は?",
49
- new String[]{"骨川", "馬川"}),
50
+ new String[]{"骨川", "馬川", "スネ山"}),
50
51
  new Quiz("ドラえもんの好物はどら焼き、では妹のドラミちゃんの好物は?",
51
52
  new String[]{"メロンパン", "チョコレートパン"}),
52
53
  new Quiz("硬式テニスで0点の事を2文字で何と言うでしょう?",
@@ -60,9 +61,8 @@
60
61
  private Deque<Quiz> quizStack = new ArrayDeque<Quiz>();
61
62
  private Quiz quiz; //出題中のクイズ
62
63
 
63
- static private final int ANSWERS_SIZE = 2;
64
64
  private JLabel questionLabel;
65
- private JButton[] answerButtons = new JButton[ANSWERS_SIZE];
65
+ private AnswerPanel answerPanel;
66
66
  private SouthPanel southPanel;
67
67
 
68
68
  public QuizGame(String title, int width, int height) {
@@ -81,14 +81,7 @@
81
81
  questionLabel.setHorizontalAlignment(JLabel.CENTER);
82
82
  add(questionLabel, BorderLayout.CENTER);
83
83
 
84
- JPanel answerPanel = new JPanel();
85
- for(int i=0; i<answerButtons.length; i++) {
86
- answerButtons[i] = new JButton();
87
- answerButtons[i].addActionListener(e -> {
88
- decide(((JButton)e.getSource()).getText());
89
- });
90
- answerPanel.add(answerButtons[i]);
84
+ answerPanel = new AnswerPanel(answer->decide(answer));
91
- }
92
85
 
93
86
  southPanel = new SouthPanel(answerPanel, e->drawQuiz(), e->end());
94
87
  add(southPanel, BorderLayout.SOUTH);
@@ -101,6 +94,33 @@
101
94
  drawQuiz();
102
95
  }
103
96
 
97
+ //回答ボタン群
98
+ static private class AnswerPanel extends JPanel {
99
+ interface AnswerListener {
100
+ void decide(String answer);
101
+ }
102
+ private AnswerListener answerListener;
103
+ private final ActionListener actionListener = new ActionListener() {
104
+ @Override
105
+ public void actionPerformed(ActionEvent e) {
106
+ String answer = ((JButton)e.getSource()).getText();
107
+ answerListener.decide(answer);
108
+ }
109
+ };
110
+ AnswerPanel(AnswerListener answerListener) {
111
+ this.answerListener = answerListener;
112
+ }
113
+ //回答数に応じてボタン配置
114
+ void setAnswers(String[] answers) {
115
+ removeAll();
116
+ for(int i=0; i<answers.length; i++) {
117
+ JButton button = new JButton(answers[i]);
118
+ button.addActionListener(actionListener);
119
+ add(button);
120
+ }
121
+ }
122
+ }
123
+
104
124
  //下部の表示を進行状況に応じて切り替える
105
125
  static private class SouthPanel extends JPanel {
106
126
  enum Card {
@@ -144,9 +164,7 @@
144
164
 
145
165
  questionLabel.setText(quiz.question);
146
166
  questionLabel.setForeground(Color.red);
147
- for(int i=0; i<answerButtons.length; i++) {
148
- answerButtons[i].setText(quiz.answers[i]);
167
+ answerPanel.setAnswers(quiz.answers);
149
- }
150
168
  }
151
169
 
152
170
  //判定

2

反映が中途でした><

2019/09/10 09:54

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -65,8 +65,8 @@
65
65
  private JButton[] answerButtons = new JButton[ANSWERS_SIZE];
66
66
  private SouthPanel southPanel;
67
67
 
68
- public QuizGame(String title,int width,int height) {
68
+ public QuizGame(String title, int width, int height) {
69
- super();
69
+ super(title);
70
70
  setVisible(true);
71
71
  setSize(width, height);
72
72
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

1

GAME クラスを反映

2019/09/10 09:01

投稿

jimbe
jimbe

スコア13352

answer CHANGED
@@ -22,8 +22,7 @@
22
22
 
23
23
  public class QuizGame extends JFrame {
24
24
  public static void main(String[] args){
25
- QuizGame gm = new QuizGame();
25
+ new QuizGame("NOBUクイズ", 500, 500);
26
- gm.setVisible(true);
27
26
  }
28
27
 
29
28
  static private class Quiz {
@@ -66,10 +65,12 @@
66
65
  private JButton[] answerButtons = new JButton[ANSWERS_SIZE];
67
66
  private SouthPanel southPanel;
68
67
 
68
+ public QuizGame(String title,int width,int height) {
69
+ super();
69
- public QuizGame() {
70
+ setVisible(true);
71
+ setSize(width, height);
70
- setDefaultCloseOperation(EXIT_ON_CLOSE);
72
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71
- setTitle("NOBUクイズ");
72
- setSize(500, 500);
73
+ setResizable(false);
73
74
 
74
75
  JPanel headerPanel = new JPanel();
75
76
  headerPanel.setBackground(Color.BLACK);
@@ -84,7 +85,7 @@
84
85
  for(int i=0; i<answerButtons.length; i++) {
85
86
  answerButtons[i] = new JButton();
86
87
  answerButtons[i].addActionListener(e -> {
87
- decide(((JButton)(e.getSource())).getText());
88
+ decide(((JButton)e.getSource()).getText());
88
89
  });
89
90
  answerPanel.add(answerButtons[i]);
90
91
  }