当我尝试使用redbull作为用户名和pass123作为密码登录时,它不显示已授予访问权限的消息,而是转到else语句并显示访问被拒绝的消息 Label usernameLb = new Label("Username:"); GridPane.setConstraints(usernameLb, 0, 0); TextField usernameTf = new TextField(); usernameTf.setPromptText("Username"); GridPane.setConstraints(usernameTf , 1, 0); Label passLb = new Label("Password:"); GridPane.setConstraints(passLb, 0, 1); TextField passTf = new TextField(); passTf.setPromptText("Password"); GridPane.setConstraints(passTf, 1, 1); Button loginBtn = new Button("Login"); loginBtn.setOnAction(e -> { String username = usernameTf.getText(); String password = passTf.getText(); if (username == "redbull" && password == "pass123"){ System.out.println(username + "\n" + password); JOptionPane.showMessageDialog(null, "Access Granted", " ", JOptionPane.PLAIN_MESSAGE); } else { System.out.println(username + "\n" + password); JOptionPane.showMessageDialog(null, "Access Denied", " ", JOptionPane.PLAIN_MESSAGE); } }); GridPane.setConstraints(loginBtn, 1, 2); gridPane.getChildren().addAll(usernameLb, usernameTf, passLb, passTf, loginBtn); Scene scene1 = new Scene(gridPane, 300, 200); window.setScene(scene1); window.show();}
1 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
.equals()
比较两个字符串的值时使用。
(username.equals("redbull") && password.equals("pass123"))
添加回答
举报
0/150
提交
取消