javafx:尝试正确关闭窗口时的错误

时间:2015-10-03 02:01:09

标签: java javafx

我正在为我的桌面应用程序设计关闭窗口功能。列出了该功能的高级说明:

  1. 如果我单击Exit menuItem,它会在关闭应用程序之前提示ConfirmBox用户确认是否要保存。
  2. 如果用户单击窗口上的CloseButton强制关闭窗口(即setOnCloseRequest函数),则退出menuItem事件将触发,这会将用户再次置于case(1)。
  3. 在我的ConfirmBox代码中,我有绑定ENTER键来保存东西,N键不保存东西,ESCAPE键关闭confirmBox。
  4. 我还为Exit menuItem(METAKEY + E)设置了加速器。
  5. 一切正常。但是,如果我按照这个特殊的步骤顺序,会有一个小错误。 每当我使用加速器进入Exit menuItem (即METAKEY + E)时,我按下3个键中的任意一个(ENTER,ESCAPE,N),confirmBox关闭但它再次弹出。

    我想知道为什么只在这种非常特殊的情况下才会发生这种情况?

    public class ConfirmBox {
        // answer[0] determines the need to Save
        // answer[1] determines whether to close the application or not
        private static boolean[] answer = new boolean[]{false,false};
        private static Stage window;
    
        public static boolean[] displayWarning(String title, String message){
            window = new Stage();
            window.initModality(Modality.APPLICATION_MODAL);
            window.setTitle(title);
            window.setMinWidth(300);
            Label label = new Label();
            label.setText(message);
            Button yesButton = new Button("Yes");
            Button noButton = new Button("No");
    
            // needToSave = true, close Application = true and close this confirmbox
            yesButton.setOnAction(ey ->{
                answer[0] = true;
                answer[1] = true;
                window.close();
            });
    
            // needToSave = false, close Application = true and close this confirmbox
            noButton.setOnAction(en -> {
                answer[0] = false;
                answer[1] = true;
                window.close();
            });
    
    
            // needToSave = false, close Application = false and close this confirmbox
            window.setOnCloseRequest(e -> {
                answer[0] = false;
                answer[1] = false;
                closeConfirmBox();
            });
    
            // key binding      
            window.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
                if ( e.getCode() == KeyCode.N){
                    noButton.fire();
                    e.consume(); 
    
                }
            });
    
            // bind enter key to yesButton      
            window.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
                if (ev.getCode() == KeyCode.ENTER ){  
                    yesButton.fire();
                   ev.consume(); 
                }
            });
    
    
            window.addEventFilter(KeyEvent.KEY_PRESSED, ev ->{
                if(ev.getCode()==KeyCode.ESCAPE){
                    ev.consume();
                    answer[0] = false;
                    answer[1] = false;
                    closeConfirmBox();
                }
            });
    
            VBox layout = new VBox(20);
            layout.setPadding(new Insets(20,5,20,5));
            HBox bottomLayout = new HBox(50);
            bottomLayout.setPadding(new Insets(20,5,20,5));
            bottomLayout.getChildren().addAll(yesButton,noButton);
            bottomLayout.setAlignment(Pos.CENTER);
            layout.getChildren().addAll(label,bottomLayout);
            layout.setAlignment(Pos.CENTER);
    
            Scene scene = new Scene(layout);
            window.setScene(scene);
            window.showAndWait();
            return answer;
        }
    
        public static void closeConfirmBox(){
            window.close(); 
        }
    }
    

    在我的控制器类中,这就是我设计MenuItem menuItemExit的方式。

            menuItemExit.setOnAction(new EventHandler<ActionEvent>(){
                @Override 
                public void handle(ActionEvent e){
                    //System.out.println("set stage" + primaryStage);
                    boolean[] answer;
                    boolean needToSave = false;
                    boolean closeApplication = false;
                    if(saved.get() == false){
                        answer = ConfirmBox.displayWarning("Warning", "Do you want to save your stuff?");
                        needToSave = answer[0];
                        closeApplication = answer[1];
                    }
                    if(needToSave == true){
                        menuItemSave.fire();
                    }
                    if(closeApplication== true){
                        Platform.runLater(new Runnable() {
                            public void run() {
                                close();
                            }
                        });
                    }
                }
            });
    
            primaryStage.setOnCloseRequest(e -> {
                e.consume();
                menuItemExit.fire();
            });
    
            menuItemExit.setAccelerator(new KeyCodeCombination(KeyCode.E, KeyCombination.META_DOWN));
    
        public void close(){
            this.primaryStage.close();
        }
    

0 个答案:

没有答案