首页 文章

如何关闭窗口关闭JavaFX应用程序?

提问于
浏览
61

在Swing中,您只需使用 setDefaultCloseOperation() 即可在窗口关闭时关闭整个应用程序 .

但是在JavaFX中我找不到相应的东西 . 我打开了多个窗口,如果窗口关闭,我想关闭整个应用程序 . 在JavaFX中这样做的方法是什么?

编辑:

据我所知,我可以覆盖 setOnCloseRequest() 以在窗口关闭时执行某些操作 . 问题是应该执行什么操作来终止整个应用程序?

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        stop();
    }
});

Application 中定义的 stop() 方法不执行任何操作 .

11 回答

  • 0

    当最后一个 Stage 关闭时,应用程序会自动停止 . 此时, Application 类的 stop() 方法被调用,因此您不需要等效于 setDefaultCloseOperation()

    如果要在此之前停止应用程序,可以调用 Platform.exit() ,例如在 onCloseRequest 调用中 .

    您可以在 Application 的javadoc页面上获取所有这些信息:http://docs.oracle.com/javafx/2/api/javafx/application/Application.html

  • 2

    一些提供的答案对我不起作用(关闭窗口后javaw.exe仍然运行)或者,eclipse在应用程序关闭后显示异常 .

    另一方面,这完美地运作:

    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent t) {
            Platform.exit();
            System.exit(0);
        }
    });
    
  • 22

    作为参考,这是使用Java 8的最小实现:

    @Override
    public void start(Stage mainStage) throws Exception {
    
        Scene scene = new Scene(new Region());
        mainStage.setWidth(640);
        mainStage.setHeight(480);
        mainStage.setScene(scene);
    
        //this makes all stages close and the app exit when the main stage is closed
        mainStage.setOnCloseRequest(e -> Platform.exit());
    
        //add real stuff to the scene...
        //open secondary stages... etc...
    }
    
  • -2
    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        @Override
        public void handle(WindowEvent event) {
            Platform.exit();
            System.exit(0);
        }
    }
    
  • 0

    你试过这个.. setOnCloseRequest

    setOnCloseRequest(EventHandler<WindowEvent> value)
    

    有一个example

  • 17

    使用Java 8这对我有用:

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Region());
        stage.setScene(scene);
    
        /* ... OTHER STUFF ... */
    
        stage.setOnCloseRequest(e -> {
            Platform.exit();
            System.exit(0);
        });
    }
    
  • 37

    这似乎对我有用:

    EventHandler<ActionEvent> quitHandler = quitEvent -> {
    
            System.exit(0);
    
        };
        // Set the handler on the Start/Resume button
        quit.setOnAction(quitHandler);
    
  • 70

    尝试

    System.exit(0);
    

    这应该终止主线程并结束主程序

  • 0

    对我来说只有以下工作:

    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
    
    
    
                    Platform.exit();
                    Thread start=new Thread(new Runnable() {
    
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            System.exit(0);     
                        }
                    });
                    start.start();
                }
    
            });
    
  • 3

    getContentPane.remove(jfxPanel);

    试试吧 (:

  • 0

    您必须覆盖Application实例中的“stop()”方法才能使其正常工作 . 如果你已经覆盖了空的“stop()”,那么应用程序会在最后一个阶段关闭后正常关闭(实际上,最后一个阶段必须是使其完全按预期工作的主要阶段) . 在这种情况下,不需要任何额外的Platform.exit或setOnCloseRequest调用 .

相关问题