JavaFX:ESC键在第二阶段按下而不是创建新的警报对话

时间:2016-05-21 13:27:21

标签: java javafx dialog

更新#2

大家好,lemme打破了我的想法。

当前设置:

  • 带有按钮(名为rootButton)的primaryStage,用于打开secondaryStage。
  • 带有按钮(名为closeButton)的secondaryStage,用于关闭此secondaryStage。
  • closeButton.setCancelButton(true);让ESC键在secondaryStage上按下closeButton

需要的功能:

  • secondaryStage上的ESC键应弹出一个Alert对话框 问我是否真的要关闭 secondaryStage 。 <<< 如何实施?

运行当前代码的结果:

  1. 当我按下ESC键时,警告对话框不显示 secondaryStage
  2. 命令行显示:"已按下取消"
  3. 因此,closeButton以某种方式跳过Alert对话框弹出窗口,而不是直接导致

    if (result.get() == ButtonType.CANCEL) {
        System.out.println("Cancel was pressed.");
    } 
    

    那么ESC键事件是否可能从secondaryStage传递到Alert对话框?如果是这种情况,如何哪里在“警报”对话框中正确使用此键事件?

    当前代码(在GoXr3Plus的帮助下格式化):

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Alert;
    import javafx.scene.control.Alert.AlertType;
    import javafx.scene.control.Button;
    import javafx.scene.control.ButtonType;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class Closing extends Application {
    
      // handle primaryStage
      @Override
      public void start(Stage primaryStage) {
         Button rootButton = new Button("show dialog");
    
         // rootButton
         rootButton.setOnAction(ac -> {
    
            // CloseButton
            Button closeButton = new Button("Close");
            closeButton.setCancelButton(true); // make this cancel button
            closeButton.setOnAction(a -> {
    
                // Initialize the alert
                Alert alert = new Alert(AlertType.CONFIRMATION, "You really want to quit?");
                // Show the alert
                alert.showAndWait().ifPresent(result -> {
                    if (result == ButtonType.OK)
                        System.out.println("OK was pressed.");
                    else if (result == ButtonType.CANCEL)
                        System.out.println("Cancel was pressed.");
                });
    
                closeButton.getScene().getWindow().hide();
            });
    
            StackPane dialogPane = new StackPane(closeButton);
    
            Stage secondaryStage = new Stage();
            secondaryStage.setScene(new Scene(dialogPane, 200, 200));
            secondaryStage.showAndWait();
        });
    
        StackPane rootPane = new StackPane(rootButton);
    
        Scene scene = new Scene(rootPane, 300, 250);
    
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    
    }
    

3 个答案:

答案 0 :(得分:0)

您可能需要发布所有代码或发布mvce,因为您已发布的代码完全适合我。看起来好像你在某种程度上连续两次紧急逃生。

尝试将setOnAction方法更改为以下内容,并检查输出的标准输出。

btnCancel.setOnAction((ActionEvent event) -> {
    Alert alert = new Alert(AlertType.CONFIRMATION);
    alert.setContentText("You really want to quit?");
    Optional<ButtonType> result = alert.showAndWait();
    if (result.isPresent()) {
        if (result.get() == ButtonType.OK) {
            System.out.println("OK was pressed.");
        }
        if (result.get() == ButtonType.CANCEL) {
            System.out.println("Cancel was pressed.");
        }
    }
    primaryStage.close();
});

答案 1 :(得分:0)

你的代码没有编译所以我修改它看起来更好并添加了一些评论。让我知道它是否符合你的要求:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Closing extends Application {

  // handle primaryStage
  @Override
  public void start(Stage primaryStage) {
     Button rootButton = new Button("show dialog");

     // rootButton
     rootButton.setOnAction(ac -> {

        // CloseButton
        Button closeButton = new Button("Close");
        closeButton.setCancelButton(true); // make this cancel button
        closeButton.setOnAction(a -> {

            // Initialize the alert
            Alert alert = new Alert(AlertType.CONFIRMATION, "You really want to quit?");
            // Show the alert
            alert.showAndWait().ifPresent(result -> {
                if (result == ButtonType.OK)
                    System.out.println("OK was pressed.");
                else if (result == ButtonType.CANCEL)
                    System.out.println("Cancel was pressed.");
            });

            closeButton.getScene().getWindow().hide();
        });

        StackPane dialogPane = new StackPane(closeButton);

        Stage secondaryStage = new Stage();
        secondaryStage.setScene(new Scene(dialogPane, 200, 200));
        secondaryStage.showAndWait();
    });

    StackPane rootPane = new StackPane(rootButton);

    Scene scene = new Scene(rootPane, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

}

答案 2 :(得分:0)

解决:

罗恩指出这段代码运行正常。我更新到当前版本的Java JDK 1.8.0_91 ,现在所需的ESC键按下确实打开了一个新对话框!谢谢大家的意见!