diff --git a/src/main/java/com/zhangmeng/online/exam/ui/components/ExamButtonComponent.java b/src/main/java/com/zhangmeng/online/exam/ui/components/ExamButtonComponent.java index ed42660..af0cfa8 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/components/ExamButtonComponent.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/components/ExamButtonComponent.java @@ -1,17 +1,28 @@ package com.zhangmeng.online.exam.ui.components; +import com.alibaba.fastjson2.JSONObject; +import com.zhangmeng.online.exam.ui.utils.AlertUtils; import javafx.beans.property.SimpleIntegerProperty; +import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Node; +import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.FlowPane; import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; import javafx.scene.paint.Paint; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import com.zhangmeng.online.exam.ui.service.ExamService; +import javafx.stage.Stage; +import javafx.stage.Window; + /** * 考试按钮组件 * @@ -74,12 +85,67 @@ public class ExamButtonComponent extends FlowPane { commit.setTextFill(Paint.valueOf("#FFFFFF")); commit.setPrefWidth(100); commit.setOnAction(event -> { - System.out.println("提交"); + VBox vBox = this.examComponent.getvBox(); + ObservableList children = vBox.getChildren(); + List> params = new ArrayList<>(); + for (Node child : children) { + if (child instanceof SingleChoiceComponent) { + SingleChoiceComponent question = (SingleChoiceComponent) child; + if (question.getNumber_of_responses() == 0) { + AlertUtils.alert_warning("您尚未作答该题目!题目:" + question.getQuestionLabel().getText() + ",请先作答!"); + break; + } + params.add(question.getContext()); + } + if (child instanceof MultiChoiceComponent) { + MultiChoiceComponent question = (MultiChoiceComponent) child; + if (question.getNumber_of_responses() == 0) { + AlertUtils.alert_warning("您尚未作答该题目!题目:" + question.getQuestionLabel().getText() + ",请先作答!"); + break; + } + params.add(question.getContext()); + } + if (child instanceof TrueFalseComponent) { + TrueFalseComponent question = (TrueFalseComponent) child; + if (question.getNumber_of_responses() == 0) { + AlertUtils.alert_warning("您尚未作答该题目!题目:" + question.getQuestionLabel().getText() + ",请先作答!"); + break; + } + params.add(question.getContext()); + } + if (child instanceof FillBlankComponent) { + FillBlankComponent question = (FillBlankComponent) child; + } + if (child instanceof ShortAnswerComponent) { + ShortAnswerComponent question = (ShortAnswerComponent) child; + if (question.getAnswerArea().getText().isEmpty()) { + AlertUtils.alert_warning("您尚未作答该题目!题目:" + question.getQuestionLabel().getText() + ",请先作答!"); + break; + } + Map context = question.getContext(); + context.put("answer_content", question.getAnswerArea().getText()); + params.add(context); + } + } + + String jsonString = JSONObject.toJSONString(params); + boolean flag = ExamService.submitExam(jsonString); + if (flag) { + AlertUtils.alert_msg("提交成功!"); + //closeDialog(); + } else { + AlertUtils.alert_warning("提交失败!"); + } + }); hBox.getChildren().add(commit); hBox.setAlignment(Pos.CENTER); this.getChildren().add(hBox); } - + public void closeDialog() { + Scene scene = examComponent.getScene(); + Stage window = (Stage) scene.getWindow(); + window.close(); + } } diff --git a/src/main/java/com/zhangmeng/online/exam/ui/components/ExamComponent.java b/src/main/java/com/zhangmeng/online/exam/ui/components/ExamComponent.java index 6615068..784cfcd 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/components/ExamComponent.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/components/ExamComponent.java @@ -3,6 +3,7 @@ package com.zhangmeng.online.exam.ui.components; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; +import com.zhangmeng.online.exam.ui.utils.AlertUtils; import com.zhangmeng.online.exam.ui.utils.ApiUtils; import com.zhangmeng.online.exam.ui.utils.HttpUtils; import javafx.beans.property.SimpleIntegerProperty; @@ -61,6 +62,13 @@ public class ExamComponent extends ScrollPane { params.put("id", context.get("paperId")); String userListData = HttpUtils.GET(ApiUtils.API_URL + "/paper/chooseQuestion/list", params); JSONObject jsonObject = JSON.parseObject(userListData); + + int code = jsonObject.getIntValue("code"); + if (code == 401){ + AlertUtils.alert_warning("请重新登录!"); + return; + } + JSONArray data = jsonObject.getJSONArray("data"); int total = jsonObject.getIntValue("total"); int index = 1; diff --git a/src/main/java/com/zhangmeng/online/exam/ui/components/MultiChoiceComponent.java b/src/main/java/com/zhangmeng/online/exam/ui/components/MultiChoiceComponent.java index e1313f3..0a2058e 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/components/MultiChoiceComponent.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/components/MultiChoiceComponent.java @@ -9,10 +9,7 @@ import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; /** @@ -27,17 +24,33 @@ public class MultiChoiceComponent extends VBox { private List checkBoxes = new ArrayList<>(); + private Map context = new HashMap<>(); + + public Map getContext() { + return context; + } + private HBox hBox = new HBox(); private int number_of_responses = 0;//作答次数 + private Label questionLabel; + + public Label getQuestionLabel() { + return questionLabel; + } + + public int getNumber_of_responses() { + return number_of_responses; + } + private final SimpleStringProperty result_answer = new SimpleStringProperty(); // 最终答案 public MultiChoiceComponent(String question, Set options) { Set selected = new HashSet<>(); // 题目组件 - Label questionLabel = new Label(question); + questionLabel = new Label(question); this.getChildren().add(questionLabel); @@ -76,7 +89,7 @@ public class MultiChoiceComponent extends VBox { // 布局容器 this.setSpacing(10); this.setPadding(new Insets(15)); - this.getChildren().addAll( optionsBox, submitBtn, resultLabel); +// this.getChildren().addAll( optionsBox, submitBtn, resultLabel); } diff --git a/src/main/java/com/zhangmeng/online/exam/ui/components/ShortAnswerComponent.java b/src/main/java/com/zhangmeng/online/exam/ui/components/ShortAnswerComponent.java index 3909a65..e020113 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/components/ShortAnswerComponent.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/components/ShortAnswerComponent.java @@ -46,8 +46,20 @@ public class ShortAnswerComponent extends VBox { private Label questionLabel; + public int getNumber_of_responses() { + return number_of_responses; + } + + public Label getQuestionLabel() { + return questionLabel; + } + private TextArea answerArea; + public TextArea getAnswerArea() { + return answerArea; + } + private List keywords = new ArrayList<>(); // 参考答案关键词(如 ["封装","继承","多态"]) private static final String DEFAULT_STYLE_CLASS = "button"; @@ -101,7 +113,7 @@ public class ShortAnswerComponent extends VBox { // 功能组件 - Button submitBtn = new Button("提交答案"); + Button submitBtn = new Button("温馨提示"); Label resultLabel = new Label(); // 提交事件处理 @@ -140,7 +152,8 @@ public class ShortAnswerComponent extends VBox { // 布局容器 // this.setSpacing(20); // this.setPadding(new Insets(20)); - this.getChildren().addAll(submitBtn, resultLabel); +// this.getChildren().addAll(submitBtn, resultLabel); +// this.getChildren().addAll(resultLabel); // 布局容器 this.setSpacing(10); diff --git a/src/main/java/com/zhangmeng/online/exam/ui/components/SingleChoiceComponent.java b/src/main/java/com/zhangmeng/online/exam/ui/components/SingleChoiceComponent.java index fc726da..96a0acf 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/components/SingleChoiceComponent.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/components/SingleChoiceComponent.java @@ -47,12 +47,20 @@ public class SingleChoiceComponent extends VBox { private int number_of_responses = 0;//作答次数 + public int getNumber_of_responses() { + return number_of_responses; + } + private final SimpleStringProperty result_answer = new SimpleStringProperty(); // 最终答案 private HBox hBox = new HBox(); private Label questionLabel; + public Label getQuestionLabel() { + return questionLabel; + } + public void setBackground() { this.indexButton.setStyle("-fx-background-color: #4698ed;"); } diff --git a/src/main/java/com/zhangmeng/online/exam/ui/components/TrueFalseComponent.java b/src/main/java/com/zhangmeng/online/exam/ui/components/TrueFalseComponent.java index af1fbb9..05659cc 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/components/TrueFalseComponent.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/components/TrueFalseComponent.java @@ -42,6 +42,14 @@ public class TrueFalseComponent extends VBox { private int number_of_responses = 0;//作答次数 + public int getNumber_of_responses() { + return number_of_responses; + } + + public Label getQuestionLabel() { + return questionLabel; + } + private final SimpleStringProperty result_answer = new SimpleStringProperty(); // 最终答案 private HBox hBox = new HBox(); diff --git a/src/main/java/com/zhangmeng/online/exam/ui/controller/LoginController.java b/src/main/java/com/zhangmeng/online/exam/ui/controller/LoginController.java index 7d8348d..e862666 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/controller/LoginController.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/controller/LoginController.java @@ -90,48 +90,50 @@ public class LoginController { user.setToken(token.toString()); UserService.setCurrentUser(user); - new Thread(UserService::user_type).start(); + admin_page(); + +// new Thread(UserService::user_type).start(); // Alert alert = AlertUtils.alert_msg(jsonObject.getString("message")); - LoginController.MyScheduledService myService = new LoginController.MyScheduledService(); - //myService.setDelay(Duration.seconds(5));//延迟 - myService.setPeriod(Duration.millis(500));//时间周期 - myService.setRestartOnFailure(true); - myService.setMaximumFailureCount(4);//设置最大失败次数 - - myService.progressProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Number oldValue, Number newValue) { - System.out.println("progressProperty:" + newValue.intValue()); - } - }); - - myService.valueProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Number oldValue, Number newValue) { - if (newValue != null) { - System.out.println("progressProperty:" + newValue.intValue()); - } - } - }); - - myService.lastValueProperty().addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observable, Number oldValue, Number newValue) { - - if (newValue != null) { - System.out.println("lastValueProperty:" + newValue.intValue()); - if (newValue.intValue() == 3) { - myService.cancel(); - //alert.close(); - //success(); - admin_page(); - } - } - } - }); - - myService.start(); +// LoginController.MyScheduledService myService = new LoginController.MyScheduledService(); +// //myService.setDelay(Duration.seconds(5));//延迟 +// myService.setPeriod(Duration.millis(500));//时间周期 +// myService.setRestartOnFailure(true); +// myService.setMaximumFailureCount(4);//设置最大失败次数 +// +// myService.progressProperty().addListener(new ChangeListener() { +// @Override +// public void changed(ObservableValue observable, Number oldValue, Number newValue) { +// System.out.println("progressProperty:" + newValue.intValue()); +// } +// }); +// +// myService.valueProperty().addListener(new ChangeListener() { +// @Override +// public void changed(ObservableValue observable, Number oldValue, Number newValue) { +// if (newValue != null) { +// System.out.println("progressProperty:" + newValue.intValue()); +// } +// } +// }); +// +// myService.lastValueProperty().addListener(new ChangeListener() { +// @Override +// public void changed(ObservableValue observable, Number oldValue, Number newValue) { +// +// if (newValue != null) { +// System.out.println("lastValueProperty:" + newValue.intValue()); +// if (newValue.intValue() == 3) { +// myService.cancel(); +// //alert.close(); +// //success(); +// admin_page(); +// } +// } +// } +// }); +// +// myService.start(); } else { diff --git a/src/main/java/com/zhangmeng/online/exam/ui/service/ExamService.java b/src/main/java/com/zhangmeng/online/exam/ui/service/ExamService.java new file mode 100644 index 0000000..f631452 --- /dev/null +++ b/src/main/java/com/zhangmeng/online/exam/ui/service/ExamService.java @@ -0,0 +1,30 @@ +package com.zhangmeng.online.exam.ui.service; + +import com.alibaba.fastjson2.JSONObject; +import com.zhangmeng.online.exam.ui.utils.AlertUtils; +import com.zhangmeng.online.exam.ui.utils.ApiUtils; +import com.zhangmeng.online.exam.ui.utils.HttpUtils; +import javafx.scene.control.Alert; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author zm + * @date 2025/4/19 11:43 + * @version: 1.0 + */ +public class ExamService { + public static boolean submitExam(String jsonString) { + Map map = new HashMap<>(); + map.put("json_data", jsonString); + String response = HttpUtils.POST(ApiUtils.API_URL + "/exam/saveAnswer", map); + try { + JSONObject jsonObject = JSONObject.parseObject(response); + return jsonObject.getIntValue("code") == 200; + } catch (Exception e) { + //throw new RuntimeException(e); + return false; + } + } +} diff --git a/src/main/java/com/zhangmeng/online/exam/ui/utils/ApiUtils.java b/src/main/java/com/zhangmeng/online/exam/ui/utils/ApiUtils.java index b00c598..9a85235 100644 --- a/src/main/java/com/zhangmeng/online/exam/ui/utils/ApiUtils.java +++ b/src/main/java/com/zhangmeng/online/exam/ui/utils/ApiUtils.java @@ -128,7 +128,7 @@ public class ApiUtils { } public static void submitAnswer(Map context) { - System.out.println(context); + }