use Java to achieve:A GUI-based program is required that will enable people to take a test from a bank of multiple choice questions. Each question consists of: ● the question text; ● 4 possible answers; ● the number of the correct answer (1-4, not 0-3); Questions are held sequentially in a text file with each question held on 6 consecutive lines. An example entry in the questions text file could look like this: What is your favourite colour? Red Green Blue Pink 4 How many Harry Potter books are there? 5 6 7 8 3 The programme should have the following features: ● A menu including Open and Exit where Open starts a JFileChooser to select the file with the questions inside and Exit ends the programme. ● Once a file is loaded, the GUI should display one question and its answers at a time. ● The user should be able to select an answer and they should be informed if they were correct or not. ● The user should be made aware of the number of correctly answered and the total number of questions answered. ● The user should only be able to proceed to the next question once they answered the current one. ● Once all questions have been answered, the user should be informed of their overall score and that the game has finished. The Open menu item should now be enabled to start a new quiz. Optionally, you can add a restart menu item to redo the current quiz.
时间: 2024-02-14 07:03:41 浏览: 77
Sure, I can help you with that. Here is some sample Java code that implements the GUI-based program you described:
```
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
public class MultipleChoiceQuiz implements ActionListener {
private JFrame frame;
private JFileChooser fileChooser;
private JButton openButton;
private JButton exitButton;
private JButton nextButton;
private JButton restartButton;
private JLabel questionLabel;
private JRadioButton[] answerButtons;
private ButtonGroup answerGroup;
private List<Question> questionList;
private int currentQuestionIndex;
private int numCorrect;
private int numAnswered;
public MultipleChoiceQuiz() {
frame = new JFrame("Multiple Choice Quiz");
fileChooser = new JFileChooser();
openButton = new JButton("Open");
openButton.addActionListener(this);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
nextButton = new JButton("Next");
nextButton.addActionListener(this);
restartButton = new JButton("Restart");
restartButton.addActionListener(this);
questionLabel = new JLabel();
answerButtons = new JRadioButton[4];
for (int i = 0; i < answerButtons.length; i++) {
answerButtons[i] = new JRadioButton();
}
answerGroup = new ButtonGroup();
for (JRadioButton button : answerButtons) {
answerGroup.add(button);
}
questionList = new ArrayList<>();
currentQuestionIndex = 0;
numCorrect = 0;
numAnswered = 0;
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
buttonPanel.add(openButton);
buttonPanel.add(exitButton);
JPanel answerPanel = new JPanel(new GridLayout(4, 1));
for (JRadioButton button : answerButtons) {
answerPanel.add(button);
}
JPanel questionPanel = new JPanel(new BorderLayout());
questionPanel.add(questionLabel, BorderLayout.NORTH);
questionPanel.add(answerPanel, BorderLayout.CENTER);
JPanel controlPanel = new JPanel(new GridLayout(1, 0));
controlPanel.add(nextButton);
controlPanel.add(restartButton);
frame.add(buttonPanel, BorderLayout.NORTH);
frame.add(questionPanel, BorderLayout.CENTER);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new MultipleChoiceQuiz();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openButton) {
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
questionList.clear();
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" ");
String questionText = parts[0];
String[] answers = {parts[1], parts[2], parts[3], parts[4]};
int correctAnswer = Integer.parseInt(parts[5]);
questionList.add(new Question(questionText, answers, correctAnswer));
}
currentQuestionIndex = 0;
numCorrect = 0;
numAnswered = 0;
displayCurrentQuestion();
nextButton.setEnabled(true);
restartButton.setEnabled(false);
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Error reading file: " + ex.getMessage());
}
}
} else if (e.getSource() == exitButton) {
System.exit(0);
} else if (e.getSource() == nextButton) {
if (answerGroup.getSelection() != null) {
int selectedAnswer = -1;
for (int i = 0; i < answerButtons.length; i++) {
if (answerButtons[i].isSelected()) {
selectedAnswer = i + 1;
}
}
Question currentQuestion = questionList.get(currentQuestionIndex);
if (selectedAnswer == currentQuestion.getCorrectAnswer()) {
numCorrect++;
}
numAnswered++;
if (numAnswered == questionList.size()) {
JOptionPane.showMessageDialog(frame, "You got " + numCorrect + " out of " + numAnswered + " correct.");
nextButton.setEnabled(false);
restartButton.setEnabled(true);
} else {
currentQuestionIndex++;
displayCurrentQuestion();
}
}
} else if (e.getSource() == restartButton) {
currentQuestionIndex = 0;
numCorrect = 0;
numAnswered = 0;
displayCurrentQuestion();
nextButton.setEnabled(true);
restartButton.setEnabled(false);
}
}
private void displayCurrentQuestion() {
Question currentQuestion = questionList.get(currentQuestionIndex);
questionLabel.setText(currentQuestion.getQuestionText());
String[] answers = currentQuestion.getAnswers();
for (int i = 0; i < answerButtons.length; i++) {
answerButtons[i].setText(answers[i]);
answerButtons[i].setSelected(false);
}
}
private static class Question {
private final String questionText;
private final String[] answers;
private final int correctAnswer;
public Question(String questionText, String[] answers, int correctAnswer) {
this.questionText = questionText;
this.answers = answers;
this.correctAnswer = correctAnswer;
}
public String getQuestionText() {
return questionText;
}
public String[] getAnswers() {
return answers;
}
public int getCorrectAnswer() {
return correctAnswer;
}
}
}
```
This code uses Swing to create a GUI with buttons, labels, and radio buttons. It also uses a JFileChooser to allow the user to select a file containing the questions. The program reads the questions from the file and stores them in a list of Question objects. The user can then answer the questions one by one, and their score is displayed at the end. The program also includes a "Restart" button to allow the user to start a new quiz.
阅读全文