2017-10-11 26 views
0

我有一個JavaFX應用程序,它最初將顯示一個登錄對話框,供用戶鍵入用戶名和密碼。看下面的源代碼。在JavaFX對話框中控制ESC鍵操作

如果用戶點擊「連接」按鈕,應用程序將使用輸入的用戶名和密碼執行登錄,隱藏登錄對話框,然後顯示主窗口。

如果用戶點擊「退出」按鈕或「X」關閉按鈕,將顯示一條警告以獲得用戶的確認。如果用戶確認,應用程序退出。

我的問題是當登錄對話框顯示時,用戶按Esc鍵時會發生什麼。當按下此鍵時,退出確認警報將會顯示,並在此之後立即關閉。所以我們看到每當按下Escape鍵時,退出確認對話框都會立即顯示出來。

這是怎麼發生的?

我想按下Escape鍵就等於點擊「Exit」或「X」按鈕。也就是說,按下Escape鍵時,會顯示退出確認對話框。

另外,是否可以完全禁用Escape鍵?

在此先感謝。

public class TestApp extends Application { 

    private Stage primaryStage; 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     this.primaryStage = stage; 
     HBox pane = new HBox(); 
     pane.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(pane, 300, 300); 
     stage.setScene(scene); 
     showLoginDialog(); 
    } 

    public void showLoginDialog() { 
     Dialog<String> loginDialog = new Dialog<>(); 
     loginDialog.setTitle("Login"); 
     loginDialog.setHeaderText("Enter User Name and Password to login."); 
     loginDialog.setResizable(false); 
     Label userNameLabel = new Label("User Name:"); 
     Label passwordLabel = new Label("Password:"); 
     TextField userNameField = new TextField(); 
     userNameField.setMaxWidth(Double.MAX_VALUE); 
     PasswordField passwordField = new PasswordField(); 
     passwordField.setMaxWidth(Double.MAX_VALUE); 
     GridPane grid = new GridPane(); 
     grid.setAlignment(Pos.CENTER); 
     grid.setHgap(10); 
     grid.setVgap(10); 
     grid.setPadding(new Insets(20, 35, 20, 35)); 
     grid.add(userNameLabel, 1, 1); 
     grid.add(userNameField, 2, 1); 
     grid.add(passwordLabel, 1, 2); 
     grid.add(passwordField, 2, 2); 
     loginDialog.getDialogPane().setContent(grid); 
     loginDialog.getDialogPane().getButtonTypes().add(ButtonType.OK); 
     loginDialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL); 
     Button connectButton = (Button) loginDialog.getDialogPane().lookupButton(ButtonType.OK); 
     connectButton.setText("Connect"); 
     connectButton.addEventFilter(ActionEvent.ACTION, event -> { 
      // perform login here 
      loginDialog.hide(); 
      primaryStage.show(); 
      event.consume(); 
     }); 
     Button exitButton = (Button) loginDialog.getDialogPane().lookupButton(ButtonType.CANCEL); 
     exitButton.setText("Exit"); 
     exitButton.addEventFilter(ActionEvent.ACTION, event -> {    
      handleExit(); 
      event.consume(); 
     }); 
     Stage stage = (Stage) loginDialog.getDialogPane().getScene().getWindow(); 
     stage.setOnCloseRequest(event -> { 
      handleExit(); 
      event.consume(); 
     }); 
     stage.show(); 
    } 

    private void handleExit() { 
     Alert alert = new Alert(AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO); 
     alert.setHeaderText("Confirm exit?"); 
     alert.resultProperty().addListener((observable, previous, current) -> { 
      if (current == ButtonType.YES) { 
       System.exit(1); 
      } 
     }); 
     alert.show(); 
    } 

} 
+0

代碼工作對我很好。退出按鈕只顯示確認對話框,僅此而已。 – JKostikiadis

+0

當我按下退出鍵時,確認提示出現,然後立即關閉。 – user3573403

+0

這不會發生在我身上。很奇怪......你確定你正在運行上面的代碼嗎?你的jdk版本是什麼? – JKostikiadis

回答

0

看看這個有差別:以上

private void handleExit() { 
    Alert alert = new Alert(AlertType.CONFIRMATION, "", ButtonType.YES, ButtonType.NO); 
    alert.setHeaderText("Confirm exit?"); 

    Optional<ButtonType> result = alert.showAndWait(); 
    if(result.get() == ButtonType.YES) 
    { 
     Platform.exit(); 
    } 

    alert.show(); 
} 
+0

我用你的方法替換了我的handleExit()方法。現在發生的情況是:當我按下並釋放ESC鍵時,確認提示將出現,消失,然後再次出現。 – user3573403

+0

在你的handleExit()方法中,我把一個System.out.println(「XX」);在alert.show()之前。然後,當應用程序運行時,我按下並釋放登錄對話框上的ESC按鈕。和「XX」印刷!這意味着alert.showAndWait()根本不會阻止!很奇怪! – user3573403

+0

你一定有什麼奇怪的事情發生。我會更新Java和我的IDE。 – Sedrick