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.
325 lines
11 KiB
325 lines
11 KiB
package mathlearning.ui;
|
|
|
|
import mathlearning.model.User;
|
|
import mathlearning.service.MultipleChoiceGenerator.MultipleChoiceGenerator;
|
|
import mathlearning.service.UserService;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class SelTestFrame extends JFrame {
|
|
private MainFrame mainFrame;
|
|
private User user;
|
|
private UserService userService;
|
|
private MultipleChoiceGenerator multipleChoiceGenerator;
|
|
|
|
private int questionNum;
|
|
private int currentQuestionDex;
|
|
private String[] answers;
|
|
private String[] myAnswers;
|
|
|
|
private JLabel titleLabel;
|
|
private JButton optionA;
|
|
private JButton optionB;
|
|
private JButton optionC;
|
|
private JButton optionD;
|
|
private JButton prevButton;
|
|
private JButton nextButton;
|
|
private JButton submitButton;
|
|
private JLabel question;
|
|
private JLabel choice;
|
|
|
|
public SelTestFrame(MainFrame mainFrame, User user, UserService userService, int num) {
|
|
this.mainFrame = mainFrame;
|
|
this.user = user;
|
|
this.userService = userService;
|
|
this.questionNum = num;
|
|
this.currentQuestionDex = 0;
|
|
this.myAnswers = new String[questionNum];
|
|
this.multipleChoiceGenerator = new MultipleChoiceGenerator(num, user);
|
|
this.answers = multipleChoiceGenerator.GetCorrectAnswerNo();
|
|
InitUI();
|
|
addWindowListener(new java.awt.event.WindowAdapter() {
|
|
@Override
|
|
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
|
handleCloseOperation();
|
|
}
|
|
});
|
|
}
|
|
|
|
private void InitUI() {
|
|
setTitle("答题界面");
|
|
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
|
|
setSize(800, 600);
|
|
setLocationRelativeTo(null);
|
|
|
|
//主面板
|
|
JPanel mainPanel = new JPanel(new BorderLayout());
|
|
|
|
//上
|
|
titleLabel = new JLabel("当前为:第" + (currentQuestionDex + 1) + "题");
|
|
|
|
//左侧面板
|
|
JPanel leftPanel = createLeftPanel();
|
|
|
|
//中
|
|
JPanel centerPanel = centerPanel();
|
|
|
|
//右
|
|
JPanel rightPanel = createRightPanel();
|
|
|
|
//底
|
|
JPanel bottomPanel = createButtonPanel();
|
|
|
|
mainPanel.add(titleLabel, BorderLayout.NORTH);
|
|
mainPanel.add(leftPanel, BorderLayout.WEST);
|
|
mainPanel.add(centerPanel, BorderLayout.CENTER);
|
|
mainPanel.add(rightPanel, BorderLayout.EAST);
|
|
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
|
|
|
|
add(mainPanel);
|
|
|
|
updateNavigationButtons();
|
|
}
|
|
|
|
private JPanel createLeftPanel() {
|
|
JPanel panel = new JPanel(new BorderLayout());
|
|
|
|
JLabel usernameLabel = new JLabel("用户:" + user.getUsername());
|
|
usernameLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
|
|
usernameLabel.setForeground(Color.BLUE);
|
|
panel.add(usernameLabel, BorderLayout.NORTH);
|
|
|
|
JList<String> questionList = new JList<>();
|
|
String[] questions = new String[questionNum + 1];
|
|
for (int i = 0; i < questionNum; i++) {
|
|
questions[i] = "题目" + (i + 1);
|
|
}
|
|
questionList.setListData(questions);
|
|
panel.add(questionList, BorderLayout.CENTER);
|
|
|
|
return panel;
|
|
}
|
|
|
|
private JPanel centerPanel() {
|
|
JPanel panel = new JPanel(new GridLayout(6, 1));
|
|
|
|
JLabel questionLabel = new JLabel("题目:");
|
|
questionLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
question = new JLabel(multipleChoiceGenerator.GetQuestionList()[currentQuestionDex]);
|
|
question.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
panel.add(questionLabel);
|
|
panel.add(question);
|
|
|
|
JLabel choiceLabel = new JLabel("选项:");
|
|
choiceLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
choice = new JLabel(multipleChoiceGenerator.GetChoiceList()[currentQuestionDex]);
|
|
choiceLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
panel.add(choiceLabel);
|
|
panel.add(choice);
|
|
|
|
return panel;
|
|
}
|
|
|
|
private JPanel createRightPanel() {
|
|
JPanel panel = new JPanel(new BorderLayout());
|
|
|
|
JLabel titleLabel = new JLabel("作答区域:");
|
|
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
|
|
panel.add(titleLabel, BorderLayout.NORTH);
|
|
|
|
JPanel buttonPanel = new JPanel(new GridLayout(4, 1));
|
|
|
|
optionA = new JButton("A");
|
|
optionA.setBackground(Color.LIGHT_GRAY);
|
|
optionA.setForeground(Color.BLACK);
|
|
optionA.addActionListener(e -> saveAnswer("A"));
|
|
optionB = new JButton("B");
|
|
optionB.setBackground(Color.LIGHT_GRAY);
|
|
optionB.setForeground(Color.BLACK);
|
|
optionB.addActionListener(e -> saveAnswer("B"));
|
|
optionC = new JButton("C");
|
|
optionC.setBackground(Color.LIGHT_GRAY);
|
|
optionC.setForeground(Color.BLACK);
|
|
optionC.addActionListener(e -> saveAnswer("C"));
|
|
optionD = new JButton("D");
|
|
optionD.setBackground(Color.LIGHT_GRAY);
|
|
optionD.setForeground(Color.BLACK);
|
|
optionD.addActionListener(e -> saveAnswer("D"));
|
|
buttonPanel.add(optionA);
|
|
buttonPanel.add(optionB);
|
|
buttonPanel.add(optionC);
|
|
buttonPanel.add(optionD);
|
|
panel.add(buttonPanel, BorderLayout.CENTER);
|
|
|
|
return panel;
|
|
}
|
|
|
|
private JPanel createButtonPanel() {
|
|
JPanel panel = new JPanel(new FlowLayout());
|
|
|
|
prevButton = new JButton("上一题");
|
|
prevButton.addActionListener(e -> goToPrevQuestion());
|
|
|
|
nextButton = new JButton("下一题");
|
|
nextButton.addActionListener(e -> goToNextQuestion());
|
|
|
|
submitButton = new JButton("提交");
|
|
submitButton.addActionListener(e -> goToSubmit());
|
|
|
|
panel.add(prevButton);
|
|
panel.add(nextButton);
|
|
panel.add(submitButton);
|
|
return panel;
|
|
}
|
|
|
|
private void saveAnswer(String answer) {
|
|
resetButtonColors();
|
|
|
|
switch (answer) {
|
|
case "A":
|
|
optionA.setBackground(Color.GREEN);
|
|
myAnswers[currentQuestionDex] = answer;
|
|
break;
|
|
case "B":
|
|
optionB.setBackground(Color.GREEN);
|
|
myAnswers[currentQuestionDex] = answer;
|
|
break;
|
|
case "C":
|
|
optionC.setBackground(Color.GREEN);
|
|
myAnswers[currentQuestionDex] = answer;
|
|
break;
|
|
case "D":
|
|
optionD.setBackground(Color.GREEN);
|
|
myAnswers[currentQuestionDex] = answer;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
private void resetButtonColors() {
|
|
optionA.setBackground(Color.LIGHT_GRAY);
|
|
optionB.setBackground(Color.LIGHT_GRAY);
|
|
optionC.setBackground(Color.LIGHT_GRAY);
|
|
optionD.setBackground(Color.LIGHT_GRAY);
|
|
}
|
|
|
|
private void goToPrevQuestion() {
|
|
if (currentQuestionDex > 0) {
|
|
currentQuestionDex--;
|
|
updateTitleLabel();
|
|
updateQuestion();
|
|
if (myAnswers[currentQuestionDex] == null) {
|
|
resetButtonColors();
|
|
}
|
|
else {
|
|
saveAnswer(myAnswers[currentQuestionDex]);
|
|
}
|
|
|
|
updateNavigationButtons();
|
|
}
|
|
}
|
|
|
|
private void goToNextQuestion() {
|
|
if (currentQuestionDex < questionNum - 1) {
|
|
currentQuestionDex++;
|
|
updateTitleLabel();
|
|
updateQuestion();
|
|
if (myAnswers[currentQuestionDex] == null) {
|
|
resetButtonColors();
|
|
} else {
|
|
saveAnswer(myAnswers[currentQuestionDex]);
|
|
}
|
|
|
|
updateNavigationButtons();
|
|
}
|
|
}
|
|
|
|
private void goToSubmit() {
|
|
int confirm = JOptionPane.showConfirmDialog(SelTestFrame.this, "你确定要提交试卷吗?", "提示", JOptionPane.YES_NO_OPTION);
|
|
if (confirm == JOptionPane.YES_OPTION) {
|
|
int correctCount = 0;
|
|
correctCount = getScore(answers, myAnswers);
|
|
|
|
double accuracy = Math.round((double) correctCount / myAnswers.length * 10000) / 100.0;
|
|
double score = Math.round((double) correctCount * 100 / myAnswers.length * 10) / 10.0;
|
|
|
|
// 弹出分数结果
|
|
String[] options = {"返回主菜单", "继续做题"};
|
|
int choice = JOptionPane.showOptionDialog(
|
|
SelTestFrame.this,
|
|
"您答对了" + correctCount + "道题,正确率为" + String.format("%.2f", accuracy) + "\n您的得分是: " + score + "分",
|
|
"测试结果",
|
|
JOptionPane.YES_NO_OPTION,
|
|
JOptionPane.INFORMATION_MESSAGE,
|
|
null,
|
|
options,
|
|
options[0]
|
|
);
|
|
|
|
if (choice == 0) {
|
|
mainFrame.setVisible(true);
|
|
dispose();
|
|
} else if (choice == 1) {
|
|
dispose();
|
|
mainFrame.setVisible(true);
|
|
mainFrame.reTryTest();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void updateNavigationButtons() {
|
|
if (currentQuestionDex == 0) {
|
|
prevButton.setEnabled(false);
|
|
prevButton.setVisible(false);
|
|
}
|
|
else {
|
|
prevButton.setEnabled(true);
|
|
prevButton.setVisible(true);
|
|
}
|
|
|
|
if (currentQuestionDex == questionNum - 1) {
|
|
nextButton.setEnabled(false);
|
|
nextButton.setVisible(false);
|
|
submitButton.setEnabled(true);
|
|
submitButton.setVisible(true);
|
|
} else {
|
|
nextButton.setEnabled(true);
|
|
nextButton.setVisible(true);
|
|
submitButton.setEnabled(false);
|
|
submitButton.setVisible(false);
|
|
}
|
|
}
|
|
|
|
private void updateTitleLabel() {
|
|
titleLabel.setText("当前为:第" + (currentQuestionDex + 1) + "题");
|
|
}
|
|
|
|
private void handleCloseOperation() {
|
|
int result = JOptionPane.showConfirmDialog(SelTestFrame.this, "确定要中途退出答题吗?当前答题进度将会丢失!", "确认退出", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
|
|
|
|
if (result == JOptionPane.YES_OPTION) {
|
|
mainFrame.setVisible(true);
|
|
dispose();
|
|
}
|
|
}
|
|
|
|
private int getScore(String[] answers, String[] myAnswers) {
|
|
if (answers == null || myAnswers == null){
|
|
return 0;
|
|
}
|
|
int score = 0;
|
|
for (int i = 0; i < answers.length; i++) {
|
|
if (answers[i] != null && answers[i].equals(myAnswers[i])){
|
|
score++;
|
|
}
|
|
}
|
|
return score;
|
|
}
|
|
|
|
private void updateQuestion() {
|
|
question.setText(multipleChoiceGenerator.GetQuestionList()[currentQuestionDex]);
|
|
choice.setText(multipleChoiceGenerator.GetChoiceList()[currentQuestionDex]);
|
|
}
|
|
}
|