使用JavaFx Fxml实现账号密码登录

复制代码
HelloApplication:
复制代码
package com.example.dr295cmonth7;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 0, 0);
        stage.setTitle("DR295C数据采集传输仪");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

login.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>

<GridPane xmlns:fx="http://javafx.com/fxml"
          fx:controller="com.example.dr295cmonth7.LoginView" alignment="center" hgap="10" vgap="10">
    <VBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0">
        <HBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0">
            <Label text="登录" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
        </HBox>
        <HBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0">
            <Label text="账号:" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
            <TextField fx:id="usernameField" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
        </HBox>
        <HBox alignment="CENTER" maxWidth="300.0" prefWidth="300.0" spacing="10.0">
            <Label text="密码:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
            <PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
        </HBox>
        <Label fx:id="welcomeText" alignment="CENTER" maxWidth="1.7976931348623157E308"
               style="-fx-text-fill: red;"/>
        <Button text="登录" onAction="#handleLogin" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
    </VBox>
</GridPane>

handleLogin.java:

package com.example.dr295cmonth7;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

public class LoginView {

    @FXML
    private TextField usernameField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private Label welcomeText;

    @FXML
    void handleLogin(ActionEvent event) {
        String username = usernameField.getText();
        String password = passwordField.getText();

        String emailPattern = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
        if (username.isEmpty() || password.length() < 6||!username.matches(emailPattern)) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            if (username.isEmpty()) {
        alert.setHeaderText("账号不能为空");
    } else if (password.isEmpty()) {
        alert.setHeaderText("密码不能为空");
    } else {
        alert.setHeaderText("账号格式不正确,应为有效的邮箱地址");
    }
    alert.showAndWait();
    return;
        }
        // 假设正确的账号为 "admin",密码为 "123456"
        if (!"admin".equals(username) ||!"123456".equals(password)) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("提示");
            alert.setHeaderText("账号或密码错误");
            alert.showAndWait();
            welcomeText.setText("登录失败");
            return;
        }
        // 登录成功的处理
        System.out.println("登录成功");
    }
}
相关推荐
uppp»42 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
Yan-英杰44 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
weixin_307779132 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥3 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ew452183 小时前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui
suibian52353 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
Moon.93 小时前
el-table的hasChildren不生效?子级没数据还显示箭头号?树形数据无法展开和收缩
前端·vue.js·html
垚垚 Securify 前沿站3 小时前
深入了解 AppScan 工具的使用:筑牢 Web 应用安全防线
运维·前端·网络·安全·web安全·系统安全
多想和从前一样5 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展