|
|
|
|
@ -0,0 +1,172 @@
|
|
|
|
|
package view;
|
|
|
|
|
|
|
|
|
|
import javax.swing.JButton;
|
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
import javax.swing.JRadioButton;
|
|
|
|
|
import javax.swing.ButtonGroup;
|
|
|
|
|
import javax.swing.BorderFactory;
|
|
|
|
|
import javax.swing.SwingConstants;
|
|
|
|
|
|
|
|
|
|
import java.awt.BorderLayout;
|
|
|
|
|
import java.awt.FlowLayout;
|
|
|
|
|
import java.awt.Font;
|
|
|
|
|
import java.awt.GridLayout;
|
|
|
|
|
|
|
|
|
|
import model.Paper;
|
|
|
|
|
import model.Login;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 答题界面(选择题,四选一)
|
|
|
|
|
*/
|
|
|
|
|
public class ExamFrame extends JFrame {
|
|
|
|
|
|
|
|
|
|
private Paper paper;
|
|
|
|
|
private Login.Account user;
|
|
|
|
|
private int index = 0;
|
|
|
|
|
private int score = 0;
|
|
|
|
|
|
|
|
|
|
private JLabel qLabel;
|
|
|
|
|
private JRadioButton[] radioBtns = new JRadioButton[4];
|
|
|
|
|
private ButtonGroup group;
|
|
|
|
|
private JButton submitBtn;
|
|
|
|
|
private JButton quitBtn;
|
|
|
|
|
|
|
|
|
|
private JPanel centerPanel;
|
|
|
|
|
private JPanel bottomPanel;
|
|
|
|
|
|
|
|
|
|
public ExamFrame(Paper paper, Login.Account user) {
|
|
|
|
|
this.paper = paper;
|
|
|
|
|
this.user = user;
|
|
|
|
|
|
|
|
|
|
setTitle("答题 - " + user.username);
|
|
|
|
|
setSize(700, 400);
|
|
|
|
|
setLocationRelativeTo(null);
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
|
|
qLabel = new JLabel("", SwingConstants.LEFT);
|
|
|
|
|
qLabel.setFont(new Font("Serif", Font.PLAIN, 16));
|
|
|
|
|
JPanel top = new JPanel(new BorderLayout());
|
|
|
|
|
top.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
|
|
|
|
top.add(qLabel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
centerPanel = new JPanel(new GridLayout(4, 1, 6, 6));
|
|
|
|
|
group = new ButtonGroup();
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
radioBtns[i] = new JRadioButton();
|
|
|
|
|
group.add(radioBtns[i]);
|
|
|
|
|
centerPanel.add(radioBtns[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
|
|
|
|
submitBtn = new JButton("提交当前题");
|
|
|
|
|
quitBtn = new JButton("结束并查看成绩");
|
|
|
|
|
bottomPanel.add(quitBtn);
|
|
|
|
|
bottomPanel.add(submitBtn);
|
|
|
|
|
|
|
|
|
|
add(top, BorderLayout.NORTH);
|
|
|
|
|
add(centerPanel, BorderLayout.CENTER);
|
|
|
|
|
add(bottomPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
submitBtn.addActionListener(e -> submitAnswer());
|
|
|
|
|
quitBtn.addActionListener(e -> finishExam());
|
|
|
|
|
|
|
|
|
|
showQuestion();
|
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示一道题
|
|
|
|
|
*/
|
|
|
|
|
private void showQuestion() {
|
|
|
|
|
if (index >= paper.size()) {
|
|
|
|
|
showSubmitPage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String q = paper.questions.get(index);
|
|
|
|
|
qLabel.setText("<html><b>第 " + (index + 1) + " 题:</b> " + q + "</html>");
|
|
|
|
|
String[] opts = paper.options.get(index);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
radioBtns[i].setText(opts[i]);
|
|
|
|
|
}
|
|
|
|
|
// 清除上一次选择
|
|
|
|
|
group.clearSelection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交当前题
|
|
|
|
|
*/
|
|
|
|
|
private void submitAnswer() {
|
|
|
|
|
int selected = -1;
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
if (radioBtns[i].isSelected()) {
|
|
|
|
|
selected = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (selected == -1) {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "请选择一个选项后再提交");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 判断对错
|
|
|
|
|
if (selected == paper.correctIndex[index]) {
|
|
|
|
|
score++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
if (index < paper.size()) {
|
|
|
|
|
showQuestion();
|
|
|
|
|
} else {
|
|
|
|
|
// 做完所有题 → 显示提交确认页面
|
|
|
|
|
showSubmitPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 做完所有题目后显示确认提交界面
|
|
|
|
|
*/
|
|
|
|
|
private void showSubmitPage() {
|
|
|
|
|
// 移除中间题目区域和按钮
|
|
|
|
|
getContentPane().remove(centerPanel);
|
|
|
|
|
getContentPane().remove(bottomPanel);
|
|
|
|
|
|
|
|
|
|
JLabel finishLabel = new JLabel("已完成所有题目,是否确认提交?", SwingConstants.CENTER);
|
|
|
|
|
finishLabel.setFont(new Font("Serif", Font.BOLD, 18));
|
|
|
|
|
add(finishLabel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
JPanel confirmPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
|
|
|
|
JButton confirmBtn = new JButton("确认提交");
|
|
|
|
|
JButton cancelBtn = new JButton("返回最后一题");
|
|
|
|
|
confirmPanel.add(cancelBtn);
|
|
|
|
|
confirmPanel.add(confirmBtn);
|
|
|
|
|
add(confirmPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
confirmBtn.addActionListener(e -> finishExam());
|
|
|
|
|
cancelBtn.addActionListener(e -> {
|
|
|
|
|
// 返回最后一题继续查看/修改
|
|
|
|
|
getContentPane().remove(finishLabel);
|
|
|
|
|
getContentPane().remove(confirmPanel);
|
|
|
|
|
add(centerPanel, BorderLayout.CENTER);
|
|
|
|
|
add(bottomPanel, BorderLayout.SOUTH);
|
|
|
|
|
index = paper.size() - 1;
|
|
|
|
|
showQuestion();
|
|
|
|
|
revalidate();
|
|
|
|
|
repaint();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
revalidate();
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 完成考试 → 进入成绩界面
|
|
|
|
|
*/
|
|
|
|
|
private void finishExam() {
|
|
|
|
|
int total = paper.size();
|
|
|
|
|
double percent = total == 0 ? 0 : (100.0 * score / total);
|
|
|
|
|
dispose();
|
|
|
|
|
new ResultFrame(user, score, total, percent);
|
|
|
|
|
}
|
|
|
|
|
}
|