You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.6 KiB
140 lines
4.6 KiB
package view;
|
|
import controller.ExamController;
|
|
import model.Question;
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.util.List;
|
|
|
|
public class ExamFrame extends JFrame {
|
|
private List<Question> questions;
|
|
private int current = 0;
|
|
private int[] answers;
|
|
private ButtonGroup group;
|
|
private JButton nextBtn;
|
|
private JButton prevBtn;
|
|
|
|
public ExamFrame(ExamController controller, List<Question> questions) {
|
|
this.questions = questions;
|
|
this.answers = new int[questions.size()];
|
|
for (int i = 0; i < answers.length; i++) answers[i] = -1;
|
|
|
|
setTitle("考试");
|
|
setSize(500, 300);
|
|
setLocationRelativeTo(null);
|
|
|
|
JPanel panel = new JPanel(new BorderLayout());
|
|
JLabel questionLabel = new JLabel();
|
|
group = new ButtonGroup();
|
|
JRadioButton[] options = new JRadioButton[4];
|
|
JPanel optionsPanel = new JPanel(new GridLayout(4, 1));
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
options[i] = new JRadioButton();
|
|
group.add(options[i]);
|
|
optionsPanel.add(options[i]);
|
|
int index = i;
|
|
options[i].addActionListener(e -> answers[current] = index);
|
|
}
|
|
|
|
prevBtn = new JButton("上一题");
|
|
nextBtn = new JButton("下一题");
|
|
JButton submit = new JButton("提交");
|
|
JPanel buttonPanel = new JPanel();
|
|
|
|
prevBtn.addActionListener(e -> showQuestion(current - 1, questionLabel, options));
|
|
nextBtn.addActionListener(e -> {
|
|
if (current == questions.size() - 1) {
|
|
// 已经是最后一题,提示用户
|
|
JOptionPane.showMessageDialog(this,
|
|
"已经是最后一题!\n请点击提交按钮完成考试。",
|
|
"提示",
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
} else {
|
|
showQuestion(current + 1, questionLabel, options);
|
|
}
|
|
});
|
|
|
|
submit.addActionListener(e -> {
|
|
// 检查是否有未答题目
|
|
int unanswered = 0;
|
|
for (int answer : answers) {
|
|
if (answer == -1) unanswered++;
|
|
}
|
|
|
|
String message;
|
|
if (unanswered > 0) {
|
|
message = "还有 " + unanswered + " 道题目未作答,确定要提交吗?";
|
|
} else {
|
|
message = "确定要提交试卷吗?";
|
|
}
|
|
|
|
// 确认提交对话框
|
|
int result = JOptionPane.showConfirmDialog(
|
|
this,
|
|
message,
|
|
"确认提交",
|
|
JOptionPane.YES_NO_OPTION,
|
|
JOptionPane.QUESTION_MESSAGE
|
|
);
|
|
|
|
if (result == JOptionPane.YES_OPTION) {
|
|
int score = 0;
|
|
for (int i = 0; i < questions.size(); i++) {
|
|
if (answers[i] == questions.get(i).getCorrectAnswer()) score++;
|
|
}
|
|
controller.showResult(score, questions.size());
|
|
dispose();
|
|
}
|
|
});
|
|
|
|
buttonPanel.add(prevBtn);
|
|
buttonPanel.add(nextBtn);
|
|
buttonPanel.add(submit);
|
|
|
|
panel.add(questionLabel, BorderLayout.NORTH);
|
|
panel.add(optionsPanel, BorderLayout.CENTER);
|
|
panel.add(buttonPanel, BorderLayout.SOUTH);
|
|
|
|
add(panel);
|
|
showQuestion(0, questionLabel, options);
|
|
setVisible(true);
|
|
}
|
|
|
|
private void showQuestion(int index, JLabel label, JRadioButton[] options) {
|
|
if (index < 0 || index >= questions.size()) return;
|
|
current = index;
|
|
Question q = questions.get(index);
|
|
label.setText("第" + (index + 1) + "题: " + q.getContent() + " (" + (index + 1) + "/" + questions.size() + ")");
|
|
|
|
List<String> optionTexts = q.getOptions().getOptions();
|
|
|
|
// 先清空所有选项的选择状态
|
|
group.clearSelection();
|
|
|
|
// 设置选项文本,并根据当前题目的答案状态设置选择
|
|
for (int i = 0; i < 4; i++) {
|
|
options[i].setText(optionTexts.get(i));
|
|
if (answers[current] == i) {
|
|
options[i].setSelected(true);
|
|
}
|
|
}
|
|
|
|
// 更新按钮状态
|
|
updateButtonStates();
|
|
}
|
|
|
|
private void updateButtonStates() {
|
|
// 上一题按钮状态
|
|
prevBtn.setEnabled(current > 0);
|
|
|
|
// 下一题按钮状态
|
|
nextBtn.setEnabled(current < questions.size() - 1);
|
|
|
|
// 如果是最后一题,修改下一题按钮文本为提示
|
|
if (current == questions.size() - 1) {
|
|
nextBtn.setText("最后一题");
|
|
} else {
|
|
nextBtn.setText("下一题");
|
|
}
|
|
}
|
|
} |