我有一个显示结果条形图的程序。我想等到用户关闭条形图以继续下一行代码,询问他们是否要为图表输入新信息。
Scene scene = BarGraph.getBarChart();
primaryStage.setScene(scene);
primaryStage.setTitle("Bar Chart");
primaryStage.show();
repeat = JOptionPane.showConfirmDialog(null, "Would you like to enter another number?");
发生的事情是条形图的场景将打开,但不会显示任何内容,并且 JOptionPane 会立即弹出问题。如果我点击否,则显示图表,但程序结束。如果我点击是,程序会循环回到之前的对话框窗口,但不会显示图表,直到另一个对话框结束。
我想等到用户关闭条形图后再提示他们。在这一点上,我非常沮丧,我什至会人为地暂停一下,比如 wait(5000) 之类的,尽管我也无法让它发挥作用。
我已经阅读了很多,但还没有找到真正有效的解决方案。最接近的问题是:JavaFX wait till stage has been closed
据我了解,我实际上无法让程序等待,但可以设置窗口关闭后发生的操作。但是按照那里的建议只是导致舞台打开然后立即关闭。我去过其他几个兔子洞,但无济于事。
最佳答案
尝试使用 Stage.showAndWait()
而不是 Stage.show()
。
The
show()
method returns immediately regardless of the modality of the stage. Use theshowAndWait()
method if you need to block the caller until the modal stage is hidden (closed). The modality must be initialized before the stage is made visible.
As James_D points out ,您需要先创建另一个阶段,因为在主阶段使用它会导致错误。
This method must not be called on the primary stage or on a stage that is already visible.
关于java - 如何让我的程序等到 JavaFX 窗口关闭后再继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647368/