登录页面改为fxml

master
qmstyle 2025-03-31 15:08:22 +08:00
parent 45ad19eb4e
commit b81f2a1801
4 changed files with 227 additions and 14 deletions

View File

@ -22,7 +22,7 @@ public class OnlineExamApplication extends Application {
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(OnlineExamApplication.class.getResource("/fxml/login.fxml"));
Parent root = loader.load();
LoginPage loginPage = loader.getController();
Scene scene = new Scene(root);
// Scene scene = new Scene(new LoginPage());
stage.setScene(scene);

View File

@ -0,0 +1,208 @@
package com.zhangmeng.online.exam.ui.controller;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.admin.IndexPage;
import com.zhangmeng.online.exam.ui.admin.LoginPage;
import com.zhangmeng.online.exam.ui.admin.PaperPage;
import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.HashMap;
import java.util.Map;
/**
* @author zm
* @date 2025/3/31 14:33
* @version: 1.0
*/
public class LoginController {
@FXML
public TextField username;
@FXML
public TextField password;
//验证码
@FXML
public ImageView captcha_image;
@FXML
public TextField verificationCode;
@FXML
public Button loginButton;
@FXML
public void initialize() {
captcha_image.setImage(new Image(ApiUtils.API_URL + "/verificationCode/generate"));
captcha_image.setFitHeight(30);
captcha_image.setFitWidth(140);
captcha_image.setOnMouseClicked(event -> {
captcha_image.setImage(new Image(ApiUtils.API_URL + "/verificationCode/generate"));
});
username.setText("admin");
password.setText("123456");
loginButton.setOnAction(event -> {
login();
});
}
private void login() {
//TODO 登录逻辑
Map<String, Object> params = new HashMap<>();
params.put("username", username.getText());
params.put("password", password.getText());
params.put("captcha", verificationCode.getText());
String response = HttpUtils.POST(ApiUtils.API_URL + "/login", params);
JSONObject jsonObject = JSONObject.parseObject(response);
if (jsonObject.getIntValue("code") == 2001) {
Map<String, Object> data = (Map<String, Object>) jsonObject.get("data");
Object token = data.get("token");
HttpUtils.USER_INFO.put("token", token);
new Thread(this::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();
}
}
}
});
myService.start();
} else {
AlertUtils.alert_warning(jsonObject.getString("message"));
}
}
private void user_type() {
Map<String, Object> params = new HashMap<>();
params.put("token", HttpUtils.USER_INFO.get("token"));
String response = HttpUtils.GET(ApiUtils.API_URL + "/user/getUserInfo", params);
JSONObject jsonObject = JSONObject.parseObject(response);
if (jsonObject.getIntValue("code") == 200) {
Map<String, Object> data = (Map<String, Object>) jsonObject.get("data");
String type = data.get("type").toString();
HttpUtils.USER_INFO.put("type", type);
}
}
private void success() {
String type = HttpUtils.USER_INFO.get("type").toString();
switch (type) {
case "STUDENT" -> user_page();
case "ADMIN" -> admin_page();
}
}
private void user_page() {
Scene scene = loginButton.getScene();
Stage window = (Stage) scene.getWindow();
window.close();
Stage stage = new Stage();
//判断
PaperPage paperPage = new PaperPage();
scene = new Scene(paperPage, 1280, 720);
stage.setScene(scene);
stage.setTitle("在线考试系统");
stage.show();
}
private void admin_page() {
Scene scene = loginButton.getScene();
Stage window = (Stage) scene.getWindow();
window.close();
Stage stage = new Stage();
IndexPage indexPage = new IndexPage();
scene = new Scene(indexPage, 1280, 720);
stage.setScene(scene);
stage.setTitle("在线考试系统");
stage.show();
}
class MyScheduledService extends ScheduledService<Number> {
int sum = 0;
@Override
protected Task<Number> createTask() {
return new Task<Number>() {
@Override
protected Number call() throws Exception {
sum = sum + 1;
return sum;
}
@Override
protected void updateValue(Number value) {
super.updateValue(value);
// System.out.println("updateValue");
if (value.intValue() == 10) {
LoginController.MyScheduledService.this.cancel();
}
}
};
}
}
}

View File

@ -8,6 +8,8 @@ module com.zhangmeng.onlineexamui {
requires PaginationPicker;
opens com.zhangmeng.online.exam.ui to javafx.fxml;
exports com.zhangmeng.online.exam.ui;
opens com.zhangmeng.online.exam.ui to javafx.graphics;
opens com.zhangmeng.online.exam.ui.controller to javafx.fxml;
exports com.zhangmeng.online.exam.ui.controller;
}

View File

@ -8,15 +8,15 @@
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<AnchorPane prefHeight="315.0" prefWidth="527.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" >
<AnchorPane prefHeight="354.0" prefWidth="613.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.online.exam.ui.controller.LoginController">
<children>
<GridPane layoutX="485.0" layoutY="59.0" prefHeight="159.0" prefWidth="364.0" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="125.0" AnchorPane.rightAnchor="42.0" AnchorPane.topAnchor="59.0">
<GridPane layoutX="485.0" layoutY="78.0" prefHeight="179.0" prefWidth="327.0" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="125.0" AnchorPane.rightAnchor="161.0" AnchorPane.topAnchor="78.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="229.0" minWidth="10.0" prefWidth="54.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="423.0" minWidth="10.0" prefWidth="236.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="423.0" minWidth="10.0" prefWidth="80.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="229.0" minWidth="10.0" prefWidth="81.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="458.0" minWidth="10.0" prefWidth="276.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="174.0" minHeight="10.0" prefHeight="60.0" vgrow="SOMETIMES" />
@ -26,14 +26,17 @@
<children>
<Label text="账号" />
<Label text="密码" GridPane.rowIndex="1" />
<TextField prefHeight="23.0" prefWidth="100.0" GridPane.columnIndex="1" />
<PasswordField prefHeight="22.0" prefWidth="159.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="username" prefHeight="26.0" prefWidth="306.0" GridPane.columnIndex="1" />
<PasswordField fx:id="password" prefHeight="26.0" prefWidth="266.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="验证码" GridPane.rowIndex="2" />
<TextField prefHeight="23.0" prefWidth="121.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<HBox alignment="CENTER_LEFT" prefHeight="25.0" prefWidth="306.0" spacing="5.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<children>
<TextField fx:id="verificationCode" prefHeight="27.0" prefWidth="165.0" />
<ImageView fx:id="captcha_image" fitHeight="40.0" fitWidth="136.0" pickOnBounds="true" preserveRatio="true" />
</children>
</HBox>
</children>
</GridPane>
<Button layoutX="175.0" layoutY="231.0" mnemonicParsing="false" prefHeight="23.0" prefWidth="178.0" text="登录" />
<ImageView fitHeight="23.0" fitWidth="107.0" layoutX="424.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fitHeight="22.0" fitWidth="107.0" layoutX="442.0" layoutY="183.0" pickOnBounds="true" preserveRatio="true" />
<Button fx:id="loginButton" layoutX="213.0" layoutY="266.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="188.0" text="登录" />
</children>
</AnchorPane>