2017-09-29 6 views
0

私はカスタムログインダイアログボックスを大部分はhereからコピーしていますが、正常に動作しますが、資格情報でユーザーの転送を許可する前に、最大3回の表示と取得に問題があります。 3回連続して間違った資格情報が入力された場合は、プログラムが終了します。 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回を表示するためにそれを得たが、それだけで、ユーザが入力したデータ取り込み最初の時間であり、最後の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

'List enteredPasswords'を作成してから' enteredPasswords.add(usernamePassword.getValue()) 'を作成することがありますか? –

+0

こんにちは友人、soryしかし、私の助けをどのようにリストは、私のサンプル出力のように3回同じ最初の値が含まれます、私はそれを入力する新しい入力を得るための方法が必要です。 –

答えて

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.ifPresentdo-while蚊帳の外だった...だからusernamePasswordは一度しか割り当てられていたということでした。

+0

これを見る前に、この問題の別の回避策を見つけましたが、あなたの答えは私が尋ねた質問によく見えます。 –

関連する問題