generateQuestions(String level, int count);
}
\ No newline at end of file
diff --git a/src/com/mathlearning/view/QuizFrame.java b/src/com/mathlearning/view/QuizFrame.java
index 48590cf..002e833 100644
--- a/src/com/mathlearning/view/QuizFrame.java
+++ b/src/com/mathlearning/view/QuizFrame.java
@@ -1,6 +1,5 @@
package com.mathlearning.view;
-import com.mathlearning.controller.AuthController;
import com.mathlearning.controller.QuestionController;
import com.mathlearning.model.Question;
import javax.swing.*;
@@ -58,12 +57,23 @@ public class QuizFrame extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
- // 进度条
+ // 顶部面板(退出按钮和进度条)
+ JPanel topPanel = new JPanel(new BorderLayout());
+
+ JButton exitButton = new JButton("退出答题");
+ exitButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
+ exitButton.setBackground(new Color(220, 100, 100));
+ exitButton.setForeground(Color.WHITE);
+ exitButton.addActionListener(e -> exitQuiz());
+
progressBar = new JProgressBar(0, totalQuestions);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
- mainPanel.add(progressBar, BorderLayout.NORTH);
+
+ topPanel.add(exitButton, BorderLayout.WEST);
+ topPanel.add(progressBar, BorderLayout.CENTER);
+ mainPanel.add(topPanel, BorderLayout.NORTH);
// 问题面板
JPanel questionPanel = new JPanel(new BorderLayout(10, 20));
@@ -84,7 +94,7 @@ public class QuizFrame extends JFrame {
// 选项面板
JPanel optionsPanel = new JPanel(new GridLayout(4, 1, 10, 10));
optionsPanel.setBorder(
- BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "请选择答案"));
+ BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "请选择答案"));
optionButtons = new JRadioButton[4];
buttonGroup = new ButtonGroup();
@@ -98,11 +108,11 @@ public class QuizFrame extends JFrame {
final int index = i;
optionButtons[i].addActionListener(
- e -> {
- // 保存当前选择
- userSelections[currentDisplayIndex] = index;
- System.out.println("用户选择: 第" + (currentDisplayIndex + 1) + "题 -> 选项" + index);
- });
+ e -> {
+ // 保存当前选择
+ userSelections[currentDisplayIndex] = index;
+ System.out.println("用户选择: 第" + (currentDisplayIndex + 1) + "题 -> 选项" + index);
+ });
buttonGroup.add(optionButtons[i]);
optionsPanel.add(optionButtons[i]);
@@ -110,7 +120,6 @@ public class QuizFrame extends JFrame {
JPanel optionsContainer = new JPanel(new FlowLayout(FlowLayout.CENTER));
optionsContainer.add(optionsPanel);
- mainPanel.add(optionsContainer, BorderLayout.SOUTH);
// 控制面板
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
@@ -141,23 +150,7 @@ public class QuizFrame extends JFrame {
// 添加事件监听器
previousButton.addActionListener(new PreviousQuestionAction());
nextButton.addActionListener(new NextQuestionAction());
- // 添加顶部退出答题按钮
- JPanel topPanel = new JPanel(new BorderLayout());
- JButton exitButton = new JButton("退出答题");
- exitButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
- exitButton.setBackground(new Color(220, 100, 100));
- exitButton.setForeground(Color.WHITE);
- exitButton.addActionListener(e -> exitQuiz());
-
- progressBar = new JProgressBar(0, totalQuestions);
- progressBar.setValue(0);
- progressBar.setStringPainted(true);
- progressBar.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
-
- topPanel.add(exitButton, BorderLayout.WEST);
- topPanel.add(progressBar, BorderLayout.CENTER);
- mainPanel.add(topPanel, BorderLayout.NORTH);
add(mainPanel);
}
@@ -168,16 +161,16 @@ public class QuizFrame extends JFrame {
}
Question currentQuestion = allQuestions.get(currentDisplayIndex);
questionNumberLabel.setText(
- String.format("第 %d 题 / 共 %d 题", currentDisplayIndex + 1, totalQuestions));
+ String.format("第 %d 题 / 共 %d 题", currentDisplayIndex + 1, totalQuestions));
questionTextLabel.setText(
- ""
- + currentQuestion.getQuestionText()
- + "
");
+ ""
+ + currentQuestion.getQuestionText()
+ + "
");
String[] options = currentQuestion.getOptions();
for (int i = 0; i < 4; i++) {
String optionText =
- String.format(
- "%c. %s
", (char) ('A' + i), options[i]);
+ String.format(
+ "%c. %s
", (char) ('A' + i), options[i]);
optionButtons[i].setText(optionText);
}
buttonGroup.clearSelection(); // 清除当前选择状态
@@ -191,17 +184,17 @@ public class QuizFrame extends JFrame {
// 更新进度条
progressBar.setValue(currentDisplayIndex);
progressBar.setString(
- String.format(
- "%d/%d (%.0f%%)",
- currentDisplayIndex,
- totalQuestions,
- ((double) currentDisplayIndex / totalQuestions) * 100));
+ String.format(
+ "%d/%d (%.0f%%)",
+ currentDisplayIndex,
+ totalQuestions,
+ ((double) currentDisplayIndex / totalQuestions) * 100));
updateButtonStates(); // 更新按钮状态
}
private void exitQuiz() {
int result =
- JOptionPane.showConfirmDialog(this, "确定要退出答题吗?所有进度将丢失。", "退出确认", JOptionPane.YES_NO_OPTION);
+ JOptionPane.showConfirmDialog(this, "确定要退出答题吗?所有进度将丢失。", "退出确认", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
parentFrame.setVisible(true);
this.dispose();
@@ -245,8 +238,8 @@ public class QuizFrame extends JFrame {
// 如果没有选择答案,提示用户但允许继续(除了最后一题)
if (selectedIndex == -1) {
int result =
- JOptionPane.showConfirmDialog(
- QuizFrame.this, "您还没有选择答案,确定要继续下一题吗?", "确认", JOptionPane.YES_NO_OPTION);
+ JOptionPane.showConfirmDialog(
+ QuizFrame.this, "您还没有选择答案,确定要继续下一题吗?", "确认", JOptionPane.YES_NO_OPTION);
if (result != JOptionPane.YES_OPTION) {
return;
}
@@ -262,52 +255,27 @@ public class QuizFrame extends JFrame {
}
}
- private void submitAllAnswers() { // 直接在本地计算分数
- final int calculatedScore = calculateLocalScore(); // 使用final
- // 创建一个新的QuestionController来显示正确分数
- questionController =
- new QuestionController() {
- @Override
- public int getScore() {
- return calculatedScore; // 现在可以访问了
- }
-
- @Override
- public int getTotalQuestions() {
- return totalQuestions;
- }
-
- @Override
- public double getPercentage() {
- return (double) calculatedScore / totalQuestions * 100;
- }
-
- @Override
- public int[] getUserAnswers() {
- return userSelections;
- }
-
- @Override
- public java.util.List getCurrentQuestions() {
- return allQuestions;
- }
- };
- }
-
- private int calculateLocalScore() {
+ private void submitAllAnswers() {
+ // 计算分数
int score = 0;
for (int i = 0; i < totalQuestions; i++) {
- if (userSelections[i] != -1 && allQuestions.get(i).isCorrect(userSelections[i])) {
- score++;
+ if (userSelections[i] != -1) {
+ // 使用 QuestionController 来检查答案
+ questionController.setCurrentQuestionIndex(i);
+ if (questionController.submitAnswer(userSelections[i])) {
+ score++;
+ }
}
}
- return score;
+
+ // 重置到第一个问题,以便在结果显示时能正确访问
+ questionController.setCurrentQuestionIndex(0);
}
private void finishQuiz() {
- ScoreFrame scoreFrame =
- new ScoreFrame(questionController, userSelections, allQuestions, parentFrame);
+ // 使用原有的构造函数
+ ScoreFrame scoreFrame = new ScoreFrame(questionController, userSelections, allQuestions, parentFrame);
scoreFrame.setVisible(true);
this.dispose();
}
-}
+} // 添加这个闭合大括号
\ No newline at end of file
diff --git a/src/com/mathlearning/view/ScoreFrame.java b/src/com/mathlearning/view/ScoreFrame.java
index 9e93212..21e9edc 100644
--- a/src/com/mathlearning/view/ScoreFrame.java
+++ b/src/com/mathlearning/view/ScoreFrame.java
@@ -13,10 +13,10 @@ public class ScoreFrame extends JFrame {
private LevelSelectionFrame parentFrame;
public ScoreFrame(
- QuestionController questionController,
- int[] userSelections,
- List allQuestions,
- LevelSelectionFrame parent) {
+ QuestionController questionController,
+ int[] userSelections,
+ List allQuestions,
+ LevelSelectionFrame parent) {
this.parentFrame = parent;
this.questionController = questionController;
this.userSelections = userSelections;
@@ -34,13 +34,13 @@ public class ScoreFrame extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
- // Score summary
+ // Score summary - 修复这里
int score = questionController.getScore();
int total = questionController.getTotalQuestions();
- double percentage = questionController.getPercentage();
+ double percentage = (double) score / total * 100; // 手动计算百分比
JLabel scoreLabel =
- new JLabel(String.format("得分: %d/%d (%.1f%%)", score, total, percentage), JLabel.CENTER);
+ new JLabel(String.format("得分: %d/%d (%.1f%%)", score, total, percentage), JLabel.CENTER);
scoreLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 24));
Color color;
@@ -62,21 +62,22 @@ public class ScoreFrame extends JFrame {
for (int i = 0; i < allQuestions.size(); i++) {
Question q = allQuestions.get(i);
int userAnswer = userSelections[i];
- boolean isCorrect = (userAnswer != -1) && q.isCorrect(userAnswer);
+ // 修复这里:使用 getCorrectIndex() 而不是 getCorrectAnswerIndex()
+ boolean isCorrect = (userAnswer != -1) && (userAnswer == q.getCorrectIndex());
details.append(String.format("第%d题: %s\n", i + 1, q.getQuestionText()));
details.append(
- String.format(
- "你的答案: %s\n",
- userAnswer == -1
- ? "未作答"
- : (char) ('A' + userAnswer) + ". " + q.getOptions()[userAnswer]));
+ String.format(
+ "你的答案: %s\n",
+ userAnswer == -1
+ ? "未作答"
+ : (char) ('A' + userAnswer) + ". " + q.getOptions()[userAnswer]));
details.append(
- String.format(
- "正确答案: %s\n",
- (char) ('A' + q.getCorrectAnswerIndex())
- + ". "
- + q.getOptions()[q.getCorrectAnswerIndex()]));
+ String.format(
+ "正确答案: %s\n",
+ (char) ('A' + q.getCorrectIndex())
+ + ". "
+ + q.getOptions()[q.getCorrectIndex()]));
details.append(isCorrect ? "✓ 正确\n" : "✗ 错误\n");
details.append("--------------------\n");
}
@@ -108,4 +109,4 @@ public class ScoreFrame extends JFrame {
parentFrame.setVisible(true);
this.dispose();
}
-}
+}
\ No newline at end of file