2017-09-29 4 views
0

사용자 정의 로그인 대화 상자가 주로 here에서 복사되었지만 자격 증명 인 경우 사용자 전달을 허용하기 전에 userdata를 표시하고 가져 오는 데 문제가 있습니다. 잘못된 자격 증명이 연속으로 3 번 입력되면 프로그램을 닫거나 true입니다. Heres는 내 코드는 .... javafx에서 대화 상자를 반복하는 방법

//**************************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 루프에서 "결과 = dialog.ShowAndWait()"넣어 3 번 보여 그것을 가지고 있지만, 그것은 단지 사용자가 입력 한 데이터 캡처 첫 번째 시간이고 마지막 두 번의 시도가 아닙니다. 샘플 출력 :

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

'List enteredPasswords'를 만든 다음'enteredPasswords.add (usernamePassword.getValue())'를 만들까요? –

+0

안녕하세요 친구, sory하지만 나는 내 도움이 어떻게 목록 내 첫 번째 값을 동일한 샘플 값을 3 번 포함 할 것입니다, 나는 입력 한 새로운 입력 원을 얻는 방법이 필요합니다. –

답변

0

나는 표준 자바 FX 대화 상자로했는데,이 작품 : 코드와

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.ifPresentdo-while 루프 밖으로이었다이었다 ... 그래서 usernamePassword 한 번만 할당되었다.

+0

이 문제를보기 전에 다른 해결책을 찾았지만 귀하의 대답은 내가 물어 본 질문에 좋을 것 같습니다. –