自定义 javafx 登录对话框的用户名和密码始终不正确

Username and password are always incorrect with custom javafx login dialog

真的不知道在这里做什么?任何有关我的下一步行动的帮助,以便我可以完成它,运行 将不胜感激! 我希望通过使用带有此登录屏幕的组合框将此登录应用程序滚动到我的下一个 class 项目中。如果有人对我做错了什么有任何建议,或者如果我的代码可以写得更好,那就太好了。

import javafx.application.Application;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.util.Pair;
import javafx.geometry.Insets;
import java.util.Optional;


public class LoginPassword extends Application {
    @Override
    public void start(Stage primaryStage) {

        Dialog<Pair<String, String>> dialog = new Dialog<>();
        dialog.setTitle("Login");
        dialog.setHeaderText("Enter your username and password");



        ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);


        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        TextField username = new TextField();
        username.setPromptText("Username");
        PasswordField password = new PasswordField();
        password.setPromptText("Password");

        grid.add(new Label("Username:"), 0, 0);
        grid.add(username, 1, 0);
        grid.add(new Label("Password:"), 0, 1);
        grid.add(password, 1, 1);


        dialog.getDialogPane().setContent(grid);

        Optional<Pair<String, String>> input = dialog.showAndWait();
        System.out.println(input.get());


        if (username.equals("dom123")) {

            if (password.equals("d123")) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("You have been logged in");
                alert.setContentText("Welcome" + username);

                return;

            } else {
                int passwordattempts = 1;
                while (passwordattempts < 3) {
                    dialog.showAndWait();
                    passwordattempts++;
                }
            }
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText("Contact Admin");
            alert.showAndWait();

        } else {
            int usernameattempts = 1;
            while (usernameattempts < 3) {
                dialog.showAndWait();
                usernameattempts++;
            }
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText("Invalid Username");
            alert.showAndWait();
        }
    }


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





**strong text**

改变

if (username.equals("dom123")) {

    if (password.equals("d123")) {

if(String.valueOf(username.getText()).equals("dom123")){
    if(String.valueOf(password.getText()).equals("d123")){

因为您的用户名和密码变量只是对用户名文本字段和密码文本字段的引用,而不是其中存储的实际值,所以您需要使用 getText() 方法获取存储在其中的文本。