2025年4月19日15:19:15

master
qmstyle 2025-04-19 15:19:27 +08:00
parent d0afcabd0c
commit 0ff3a41422
9 changed files with 199 additions and 51 deletions

View File

@ -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<Node> children = vBox.getChildren();
List<Map<String, Object>> 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<String, Object> 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();
}
}

View File

@ -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;

View File

@ -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<CheckBox> checkBoxes = new ArrayList<>();
private Map<String,Object> context = new HashMap<>();
public Map<String, Object> 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<String> options) {
Set<String> 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);
}

View File

@ -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<String> 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);

View File

@ -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;");
}

View File

@ -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();

View File

@ -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<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("progressProperty:" + newValue.intValue());
}
});
myService.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (newValue != null) {
System.out.println("progressProperty:" + newValue.intValue());
}
}
});
myService.lastValueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> 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<Number>() {
// @Override
// public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
// System.out.println("progressProperty:" + newValue.intValue());
// }
// });
//
// myService.valueProperty().addListener(new ChangeListener<Number>() {
// @Override
// public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
// if (newValue != null) {
// System.out.println("progressProperty:" + newValue.intValue());
// }
// }
// });
//
// myService.lastValueProperty().addListener(new ChangeListener<Number>() {
// @Override
// public void changed(ObservableValue<? extends Number> 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 {

View File

@ -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<String, Object> 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;
}
}
}

View File

@ -128,7 +128,7 @@ public class ApiUtils {
}
public static void submitAnswer(Map<String, Object> context) {
System.out.println(context);
}