我有一个自定义登录对话框,主要是从here复制它工作正常,但我面临的问题是,如果凭据为真或关闭,在允许用户转发之前将其显示并获取用户数据最多3次如果连续3次输入错误的凭证,则为该程序。 继承我的代码......
//**************************login form*********************************
// Create the custom dialog.
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("Login");
dialog.setHeaderText("Welcome to MHI - LIS");
// Set the button types.
ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
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);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
username.textProperty().addListener((observable, oldValue, newValue) -> {
loginButton.setDisable(newValue.trim().isEmpty());
});
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> username.requestFocus());
// Convert the result to a username-password-pair when the login button is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(username.getText(), password.getText());
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
result.ifPresent(usernamePassword -> {
out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
int tryCount = 0;
boolean check_login = true;
do{
if(login(usernamePassword.getKey(),usernamePassword.getValue())){
check_login=false;
tryCount=3;
}else {
++tryCount;
username.clear();
password.clear();
result= dialog.showAndWait();
}
}while(check_login== true && tryCount < 3);
//if(check_login) closeProgram();
});//***************************End of login form**********************
现在我通过在do-while循环中输入“result = dialog.ShowAndWait()”来显示3次,但它只捕获用户第一次输入的数据而不是最后2次尝试。样本输出:
m1223 //password captured on 1st attempt
m1223//password captured on 2nd attempt but input was m222
m1223//password captured on 3rd attempt but input was m444
如何让它在每次试验中重新获得......?提前谢谢。
答案 0 :(得分:0)
我尝试使用标准的JavaFX对话框,这有效:
TextInputDialog dialog = new TextInputDialog("password");
dialog.setContentText("Please enter your password:");
String pwd = null;
int numAttempted = 0;
int MAX_ATTEMPTS = 3;
Optional<String> result;
boolean isCorrectPwd = false;
do {
result = dialog.showAndWait();
numAttempted++;
if (result.isPresent()) {
String res = result.get();
isCorrectPwd = login(res);
if (isCorrectPwd) {
pwd = res;
System.out.println("CORRECT PASSWORD: " + res);
} else {// try again -> loop
System.out.println("WRONG PASSWORD: " + res);
}
}
} while (numAttempted < MAX_ATTEMPTS && !isCorrectPwd);
您的代码存在问题,result.ifPresent
已超出do-while
循环...因此usernamePassword
仅被分配一次。