|
| |
Finally, there's the Option Dialog, which has lots (lots!) of flexibility.
Here's one example:
package swingExamples;
import javax.swing.JOptionPane;
public class TriviaGame1
{
public static void main(String[] args)
{
boolean playAgain = true;
while (playAgain)
{
playAgain = play();
}
}
private static boolean play()
{
int score = 0;
String[] choices = null;
int answer = -1;
int selection = JOptionPane.CLOSED_OPTION;
// Questions 1 through 3...
// Question 4 for sports
choices = new String[]
{
"USC", "Notre Dame", "North Carolina"
};
answer = 1; // I don't know, so this is random...
selection = JOptionPane.showOptionDialog(
null,
"\nWhich team broke UCLA basketball's " +
"\n88 game win streak?",
"Question 4 of 5",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]
);
if (selection == answer)
{
score++;
}
// Questions 5 on...
int response = JOptionPane.showConfirmDialog(
null,
"Your score was " + score +
"\n\nDo you want to play again?",
"Score",
JOptionPane.YES_NO_OPTION);
boolean playAgain = false;
if (response == JOptionPane.YES_OPTION)
{
playAgain = true;
}
return playAgain;
}
}
|
which brings up the following dialog:

and the user can choose to click the appropriate button.
At the end of the game, the user gets prompted:

Getting More Sophisticated
But if there are a number of options, it may be more appropriate to get
more sophisticated:
package swingExamples;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class TriviaGame2
{
public static void main(String[] args)
{
boolean playAgain = true;
while (playAgain)
{
playAgain = play();
}
}
private static boolean play()
{
int score = 0;
JRadioButton[] choices = null;
int answer = -1;
int selection = -1;
// Questions 1 through 3...
// Question 4 for sports
choices = new JRadioButton[]
{
new JRadioButton("USC"),
new JRadioButton("Notre Dame"),
new JRadioButton("North Carolina")
};
answer = 1; // I don't know, so this is random...
OptionsPanel optionsPanel =
new OptionsPanel(
"Which team broke UCLA basketball's " +
"\n88 game win streak?",
choices);
JOptionPane.showOptionDialog(
null,
optionsPanel,
"Question 4 of 5",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
null,
null
);
selection = optionsPanel.getSelectedOption();
if (selection == answer)
{
score++;
}
// Questions 5 on...
int response = JOptionPane.showConfirmDialog(
null,
"Your score was " + score +
"\n\nDo you want to play again?",
"Score",
JOptionPane.YES_NO_OPTION);
boolean playAgain = false;
if (response == JOptionPane.YES_OPTION)
{
playAgain = true;
}
return playAgain;
}
}
class OptionsPanel extends JPanel
{
public OptionsPanel(String question, JRadioButton[] buttons)
{
setLayout(new BorderLayout());
JTextArea questionArea = new JTextArea(question);
questionArea.setEditable(false);
questionArea.setBackground(getBackground());
add(questionArea, BorderLayout.NORTH);
JPanel answersPanel = new JPanel();
answersPanel.setLayout(new GridLayout(buttons.length, 1));
add(answersPanel, BorderLayout.CENTER);
for (int i = 0; i < buttons.length; i++)
{
if (i == 0)
{
buttons[i].setSelected(true);
}
answersPanel.add(buttons[i]);
m_group.add(buttons[i]);
}
m_buttons = buttons;
}
public int getSelectedOption()
{
for (int i = 0; i < m_buttons.length; i++)
{
if (m_buttons[i].isSelected())
{
return i;
}
}
return -1; // Should never happen
}
private ButtonGroup m_group = new ButtonGroup();
private JRadioButton[] m_buttons;
}
|
which produces:

|