gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:37:07 26 4
gpt4 key购买 nike

真的不知道在这里做什么?任何有关我下一步行动的帮助,以便我能够启动并运行它,我将不胜感激!我希望通过使用带有此登录屏幕的组合框将此登录应用程序滚动到我的下一个类(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() 方法获取存储在其中的文本。

关于java - 自定义 javafx 登录对话框的用户名和密码始终不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42927699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com