鱼C论坛

 找回密码
 立即注册
查看: 548|回复: 7

[吹水] java

[复制链接]
发表于 2023-12-31 12:41:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
@FishC
package com.example.javafxmw;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.jdbc.MysqlDataSource;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) {
        // 创建菜单栏和菜单项
        MenuBar menuBar = new MenuBar();
        Menu fileMenu = new Menu("File");
        menuBar.getMenus().addAll(fileMenu);

        VBox vBox = new VBox();
        vBox.setSpacing(10);

        // 创建账号输入框
        HBox accountBox = new HBox();
        Label accountLabel = new Label("账号:");
        TextField accountField = new TextField();
        accountBox.getChildren().addAll(accountLabel, accountField);

        // 创建密码输入框
        HBox passwordBox = new HBox();
        Label passwordLabel = new Label("密码:");
        PasswordField passwordField = new PasswordField();
        passwordBox.getChildren().addAll(passwordLabel, passwordField);

        // 创建登录按钮
        Button loginButton = new Button("登录");
        loginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 在这里编写登录逻辑
               
            }
        });

        // 创建注册按钮
        Button registerButton = new Button("注册");
        registerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 在这里编写注册逻辑
            }
        });

        // 创建单选按钮组
        ToggleGroup toggleGroup = new ToggleGroup();

        RadioButton adminRadioButton = new RadioButton("管理员");
        adminRadioButton.setToggleGroup(toggleGroup);
        adminRadioButton.setSelected(true); // 默认选中管理员登录

        RadioButton teacherRadioButton = new RadioButton("教师");
        teacherRadioButton.setToggleGroup(toggleGroup);

        // 将登录按钮和注册按钮添加到水平布局中
        HBox buttonsBox = new HBox();
        buttonsBox.getChildren().addAll(loginButton, registerButton);
        buttonsBox.setSpacing(10);

        // 将单选按钮添加到水平布局中
        HBox radioButtonsBox = new HBox();
        radioButtonsBox.getChildren().addAll(adminRadioButton, teacherRadioButton);
        radioButtonsBox.setSpacing(10);

        // 将控件添加到布局中
        vBox.getChildren().addAll(accountBox, passwordBox, buttonsBox, radioButtonsBox);

        StackPane stackPane = new StackPane();
        stackPane.setAlignment(Pos.CENTER); // 设置内容居中显示
        stackPane.getChildren().add(vBox);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(menuBar);
        borderPane.setCenter(stackPane);

        Scene scene = new Scene(borderPane, 800, 640);
        stage.setTitle("教师岗位管理系统");
        stage.setScene(scene);
        stage.show();
    }

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

这是我教师岗位管理系统的初始界面,我现在想把整个登录功能放到界面中央,帮我调整
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-31 12:41:18 | 显示全部楼层
你好!看起来你想将登录功能放到界面中央。你可以使用StackPane容器来实现这个效果。我会给你演示一下如何修改代码。

首先,将VBox移动到StackPane中,并设置StackPane的对齐方式为居中显示。代码如下所示:
StackPane stackPane = new StackPane();
stackPane.setAlignment(Pos.CENTER); // 设置内容居中显示
stackPane.getChildren().add(vBox);

然后,将StackPane添加到BorderPane的中心位置。代码如下所示:
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(stackPane);

最后,将Scene的宽度和高度调整为合适的值(例如800x640)。代码如下所示:
Scene scene = new Scene(borderPane, 800, 640);

完成以上修改后,你的登录功能将会在界面中央显示。希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-31 12:44:13 | 显示全部楼层
@FishC
我就是这样写的,并不能实现,请修改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-31 12:44:23 | 显示全部楼层
就當我是浮誇吧 发表于 2023-12-31 12:44
@FishC
我就是这样写的,并不能实现,请修改


当然,请告诉我你的问题和你想实现的功能,我会尽力帮助你。同时,请提供你的代码片段,这样我可以更好地理解你的问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-31 12:46:07 | 显示全部楼层

@FishC
package com.example.javafxmw;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.jdbc.MysqlDataSource;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) {
        // 创建菜单栏和菜单项
        MenuBar menuBar = new MenuBar();
        Menu fileMenu = new Menu("File");
        menuBar.getMenus().addAll(fileMenu);

        VBox vBox = new VBox();
        vBox.setSpacing(10);

        // 创建账号输入框
        HBox accountBox = new HBox();
        Label accountLabel = new Label("账号:");
        TextField accountField = new TextField();
        accountBox.getChildren().addAll(accountLabel, accountField);

        // 创建密码输入框
        HBox passwordBox = new HBox();
        Label passwordLabel = new Label("密码:");
        PasswordField passwordField = new PasswordField();
        passwordBox.getChildren().addAll(passwordLabel, passwordField);

        // 创建登录按钮
        Button loginButton = new Button("登录");
        loginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 在这里编写登录逻辑
               
            }
        });

        // 创建注册按钮
        Button registerButton = new Button("注册");
        registerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 在这里编写注册逻辑
            }
        });

        // 创建单选按钮组
        ToggleGroup toggleGroup = new ToggleGroup();

        RadioButton adminRadioButton = new RadioButton("管理员");
        adminRadioButton.setToggleGroup(toggleGroup);
        adminRadioButton.setSelected(true); // 默认选中管理员登录

        RadioButton teacherRadioButton = new RadioButton("教师");
        teacherRadioButton.setToggleGroup(toggleGroup);

        // 将登录按钮和注册按钮添加到水平布局中
        HBox buttonsBox = new HBox();
        buttonsBox.getChildren().addAll(loginButton, registerButton);
        buttonsBox.setSpacing(10);

        // 将单选按钮添加到水平布局中
        HBox radioButtonsBox = new HBox();
        radioButtonsBox.getChildren().addAll(adminRadioButton, teacherRadioButton);
        radioButtonsBox.setSpacing(10);

        // 将控件添加到布局中
        vBox.getChildren().addAll(accountBox, passwordBox, buttonsBox, radioButtonsBox);

        StackPane stackPane = new StackPane();
        stackPane.setAlignment(Pos.CENTER); // 设置内容居中显示
        stackPane.getChildren().add(vBox);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(menuBar);
        borderPane.setCenter(stackPane);

        Scene scene = new Scene(borderPane, 800, 640);
        stage.setTitle("教师岗位管理系统");
        stage.setScene(scene);
        stage.show();
    }

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

这是我教师岗位管理系统的初始界面,我现在想把整个登录功能放到界面中央,帮我调整
我已经设置了将VBox移动到StackPane中,并设置StackPane的对齐方式为居中显示,将StackPane添加到BorderPane的中心位置。
并不能实现
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-31 12:46:16 | 显示全部楼层
就當我是浮誇吧 发表于 2023-12-31 12:46
@FishC
package com.example.javafxmw;

import ja ...


你的做法基本上是正确的,但是要注意StackPane默认情况下会将子节点调整到最顶层显示,所以可能会遮挡住其他节点。
你可以将vBox直接设置给BorderPane的中心位置,然后设置alignment为Pos.CENTER,这样就可以实现让vBox居中显示了。
// 将控件添加到布局中
// vBox.getChildren().addAll(accountBox, passwordBox, buttonsBox, radioButtonsBox);

BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(vBox);
borderPane.setAlignment(vBox, Pos.CENTER); // 设置vBox居中显示

Scene scene = new Scene(borderPane, 800, 640);
stage.setTitle("教师岗位管理系统");
stage.setScene(scene);
stage.show();

这样修改之后,你的登录功能应该可以在界面中央正常显示了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-31 15:44:29 | 显示全部楼层
@FishC
package com.example.javafxmw;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.jdbc.MysqlDataSource;

public class HelloApplication  extends Application{
    public TextField accountField = new TextField();
    public PasswordField passwordField = new PasswordField();
    ToggleGroup toggleGroup = new ToggleGroup();
    RadioButton adminRadioButton = new RadioButton("管理员");
    @Override
    public void start(Stage stage) {

        VBox vBox = new VBox();
        vBox.setSpacing(10);

        // 创建账号输入框
        HBox accountBox = new HBox();
        Label accountLabel = new Label("账号:");
        accountBox.getChildren().addAll(accountLabel, accountField);

        // 创建密码输入框
        HBox passwordBox = new HBox();
        Label passwordLabel = new Label("密码:");
        passwordBox.getChildren().addAll(passwordLabel, passwordField);

        // 创建登录按钮
        Button loginButton = new Button("登录");
        loginButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 登录功能
                login();
            }
        });

        // 创建注册按钮
        Button registerButton = new Button("注册");
        registerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 注册功能,管理员账号写死,不可注册和修改,教师账号可注册
                register();
            }
        });

        // 创建单选按钮

        adminRadioButton.setToggleGroup(toggleGroup);
        adminRadioButton.setSelected(true); // 默认选中管理员登录

        RadioButton teacherRadioButton = new RadioButton("教师");
        teacherRadioButton.setToggleGroup(toggleGroup);

        // 将登录按钮和注册按钮添加到水平布局中
        HBox buttonsBox = new HBox();
        buttonsBox.getChildren().addAll(loginButton, registerButton);
        buttonsBox.setSpacing(130);

        // 将单选按钮添加到水平布局中
        HBox radioButtonsBox = new HBox();
        radioButtonsBox.getChildren().addAll(adminRadioButton, teacherRadioButton);
        radioButtonsBox.setSpacing(100);

        // 将控件添加到布局中
        vBox.getChildren().addAll(accountBox, passwordBox, buttonsBox, radioButtonsBox);
        BorderPane.setMargin(vBox, new Insets(180, 250, 180, 250));


        BorderPane borderPane = new BorderPane();
        borderPane.setAlignment(vBox, Pos.CENTER); // 设置vBox居中显示
        borderPane.setCenter(vBox);

        Scene scene = new Scene(borderPane, 800, 640);
        stage.setTitle("教师岗位管理系统");
        stage.setScene(scene);
        stage.show();
    }

    // 数据库连接
    private Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://localhost:3306/teacherportsystem";
        String username = "root";
        String password = "1542";
        return DriverManager.getConnection(url , username , password);
    }


    //登录方法
    public void login() {
        String account = accountField.getText();
        String password = passwordField.getText();

        // 判断选择的身份
        boolean isAdmin = toggleGroup.getSelectedToggle().equals(adminRadioButton);
        String tableName = isAdmin ? "admin" : "teacher";

        // 从对应的表中查询账号和密码
        String sql = "SELECT * FROM " + tableName + " WHERE account = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, account);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                String dbPassword = rs.getString("password");
                // 对用户输入的密码进行加密,比对数据库中的密码是否一致
                if (password.equals(dbPassword)) {
                    // 登录成功,弹出提示框
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("提示");
                    alert.setHeaderText(null);
                    alert.setContentText("登录成功!");
                    alert.showAndWait();

                    // 展示新的界面
                    showMainScene();
                } else {
                    // 密码错误,弹出提示框
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setTitle("错误");
                    alert.setHeaderText(null);
                    alert.setContentText("用户名或密码错误!");
                    alert.showAndWait();
                }
            } else {
                // 账号不存在,弹出提示框
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText(null);
                alert.setContentText("用户名或密码错误!");
                alert.showAndWait();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void showMainScene() {

    }

    public void register() {
        String account = accountField.getText();
        String password = passwordField.getText();

        // 检查用户名是否已存在
        if (checkNameExist(account)) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("用户名已存在!");
            alert.showAndWait();
            return;
        }

        // 注册新用户
        if (createTeacherAccount(account, password)) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText(null);
            alert.setContentText("注册成功!");
            alert.showAndWait();
        } else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("注册失败!");
            alert.showAndWait();
        }
    }


    private boolean createTeacherAccount(String account, String password) {
        String sql = "INSERT INTO teacher (account, password) VALUES (?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, account);
            stmt.setString(2, password);
            int result = stmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private boolean checkNameExist(String name) {
        String sql = "SELECT COUNT(*) FROM teacher WHERE account = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        launch();
    }
}
帮我重新设计注册方法,注册需要让用户在新的对话框中填写用户名和密码,还要保证用户名唯一

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-31 15:44:49 | 显示全部楼层
就當我是浮誇吧 发表于 2023-12-31 15:44
@FishC
package com.example.javafxmw;

import ja ...


重新设计注册方法,让用户在新的对话框中填写用户名和密码,并确保用户名唯一。可以按照以下步骤进行:

1. 创建一个新的对话框窗口,用于用户输入用户名和密码。
2. 在对话框中添加文本框和密码框,用于接收用户输入。
3. 添加确认按钮,当用户点击确认按钮时,获取用户名和密码。
4. 在注册方法中调用对话框来获取用户输入的用户名和密码。
5. 在注册方法中,使用checkNameExist()方法来检查用户名是否已存在。
6. 若用户名不存在,则调用createTeacherAccount()方法来注册用户。

下面是具体实现的代码:
public void register() {
    // 创建对话框窗口
    Dialog<Pair<String, String>> dialog = new Dialog<>();
    dialog.setTitle("注册用户");
    dialog.setHeaderText(null);

    // 设置对话框按钮
    ButtonType registerButtonType = new ButtonType("注册", ButtonBar.ButtonData.OK_DONE);
    dialog.getDialogPane().getButtonTypes().addAll(registerButtonType, ButtonType.CANCEL);

    // 创建布局面板
    GridPane gridPane = new GridPane();
    gridPane.setHgap(10);
    gridPane.setVgap(10);
    gridPane.setPadding(new Insets(20, 150, 10, 10));

    // 创建用户名输入框和标签
    TextField usernameField = new TextField();
    usernameField.setPromptText("请输入用户名");
    PasswordField passwordField = new PasswordField();
    passwordField.setPromptText("请输入密码");

    // 将输入框和标签添加到布局面板中
    gridPane.add(new Label("用户名:"), 0, 0);
    gridPane.add(usernameField, 1, 0);
    gridPane.add(new Label("密码:"), 0, 1);
    gridPane.add(passwordField, 1, 1);

    dialog.getDialogPane().setContent(gridPane);

    // 获取输入框的值
    dialog.setResultConverter(dialogButton -> {
        if (dialogButton == registerButtonType) {
            return new Pair<>(usernameField.getText(), passwordField.getText());
        }
        return null;
    });

    // 显示对话框并等待用户输入
    Optional<Pair<String, String>> result = dialog.showAndWait();

    // 检查是否有输入
    if (result.isPresent()) {
        String account = result.get().getKey();
        String password = result.get().getValue();

        // 检查用户名是否已存在
        if (checkNameExist(account)) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("用户名已存在!");
            alert.showAndWait();
            return;
        }

        // 注册新用户
        if (createTeacherAccount(account, password)) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText(null);
            alert.setContentText("注册成功!");
            alert.showAndWait();
        } else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("注册失败!");
            alert.showAndWait();
        }
    }
}

通过以上的代码修改后,当用户点击注册按钮时,会弹出一个新的对话框窗口,用户可以在该窗口中填写用户名和密码。然后,系统会检查用户名是否已存在,并根据结果进行相应的提示。如果用户名不存在,则会注册新用户并弹出注册成功的提示框。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 05:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表