【问题标题】:JavaFx: How can my primary window ask confirmation to close because of a secondary open windowJavaFx:我的主窗口如何因辅助打开窗口而要求确认关闭
【发布时间】:2017-10-12 22:02:01
【问题描述】:

在我的应用程序中,有一个主窗口和一个用于某些更改的辅助窗口。当用户想要关闭应用程序(= 主窗口)时,我想询问用户是否确认关闭更改的辅助窗口以避免丢失更改。

这段代码在我的主课上。

我的问题是我无法从我的主课中检查辅助窗口是否打开。

注意:正在我的主控制器类上创建辅助窗口。

谢谢!

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
        primaryStage.getIcons().add(new Image("image.png"));
        primaryStage.setTitle("Κάβα ποτών");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
            event.consume();
            closing();
            }
        });

    }

    public void closing() {
        //if(the secondary window is closed)
              //Platform.exit();
        //else
              //ask confirmation with confirm alert
    }

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

【问题讨论】:

  • 欢迎来到 Stack Overflow!考虑通过单击旁边的复选标记来接受下面的答案。这将帮助@HungryCosmos 给他们更多的代表和未来的用户搜索类似的问题,让他们知道这个有一个可接受的答案。

标签: java javafx confirmation


【解决方案1】:

使用isShowing 方法确定SecondaryStage 是否打开。

要使用它,您应该在创建阶段时将SecondaryStage 分配给一个变量,并通过将其公开或提供getter 来提供对它的访问。

And this is how to access controller from your Main class.

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多