如何使我的程序等到JavaFX窗口关闭后再继续? [英] How can I get make my program wait until JavaFX window has been closed before continuing?

查看:146
本文介绍了如何使我的程序等到JavaFX窗口关闭后再继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示结果图表的程序.我想等到用户关闭条形图以继续到下一行代码,询问他们是否要为图表输入新信息.

I have a program that is displaying a barchart of results. I want to wait until the user closes the bar chart to continue to the next line of code which is asking if they want to enter new information for the chart.

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.如果我单击否,则显示图表,但程序结束.如果我单击是",则程序将循环返回到较早的对话框窗口,但是直到另一个对话框结束时才显示图表.

What is happening is the scene for the bar chart will open, but nothing will be displayed and immediately JOptionPane pops up with the question. If I hit no, then the chart displays, but the program ends. If I hit yes, the program loops back to earlier dialog windows, but won't display the chart until the other dialog ends.

我要等待提示用户,直到他们关闭条形图为止.在这一点上,我感到非常沮丧,以至于我什至都设置了诸如wait(5000)之类的人为的暂停,尽管我也无法使它正常工作.

I want to wait to prompt the user until after they close the bar chart. I'm so frustrated at this point, that I'd even put an artificial pause like wait(5000) or something, though I have not been able to get that to work either.

我已经读了很多,但还没有找到实际可行的解决方案.最接近的问题是: JavaFX等到阶段关闭

I've read into this so much and have yet to find a solution that actually works. The closest question was this: JavaFX wait till stage has been closed

据我了解,我实际上不能使程序等待,但是可以将操作设置为在关闭窗口后执行.但是按照建议进行操作只会使平台打开,然后立即关闭.我去过其他几个兔子洞,但无济于事.

From what I read there is that I can't actually cause the program to wait, but can set actions to occur once the window is closed. But following the suggestions there just caused the stage to open and then immediately close. I've been down several other rabbit holes, but to no avail.

推荐答案

尝试使用Stage.showAndWait()代替Stage.show().

show() 方法都会立即返回.使用 showAndWait() 方法,如果您需要阻止调用者,直到隐藏(关闭)模态阶段.必须在阶段可见之前初始化模式.

The show() method returns immediately regardless of the modality of the stage. Use the showAndWait() 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.

- Stage的JavaFX文档

As James_D points out, you'll need to create another stage first, as using this on the primary stage will cause an error.

不得在主要阶段或已经可见的阶段上调用此方法.

This method must not be called on the primary stage or on a stage that is already visible.

这篇关于如何使我的程序等到JavaFX窗口关闭后再继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆