我没有太多信息,但也许有人面临同样的问题。我为一些 friend 写了一个应用程序,它包括一些警报对话。问题是警报没有显示在它被测试的一台电脑上(但在我的电脑上,它显示了!)。我有Windows 10和Java 1.8.0_51,测试系统有Windows 7和Java 1.8.0_60。
我用 CSS 设计了它的样式。这就是我实现它的方式(警报只是一个音频片段):
public void play(Stage stage, Callable<Void> func){
if (alert == null) {
alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Alarm");
alert.setHeaderText(LanguageLoader.getInstance().getLocalizedString("alarmDialog"));
alert.initOwner(stage);
alarm.setCycleCount(Timeline.INDEFINITE);
}
if (!alert.isShowing()) {
alarm.play();
stage.toFront();
stage.requestFocus();
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
alarm.stop();
try {
func.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
alert.show();
}
}
编辑:我也更新到 1.8.0_60,但我的系统上也没有显示对话框! 这是我得到的异常:
Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: showAndWait is not allowed during animation or layout processing
at javafx.scene.control.Dialog.showAndWait(Unknown Source)
at utils.Alarm.play(Alarm.java:38)
at note.NoteController.update(Controller.java:76)
at java.util.Observable.notifyObservers(Unknown Source)
at java.util.Observable.notifyObservers(Unknown Source)
at note.NoteModel.update(Model.java:57)
at java.util.Observable.notifyObservers(Unknown Source)
at java.util.Observable.notifyObservers(Unknown Source)
at utils.Watch.lambda$0(Clock.java:35)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(Unknown Source)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(Unknown Source)
at javafx.animation.Timeline.impl_playTo(Unknown Source)
at javafx.animation.AnimationAccessorImpl.playTo(Unknown Source)
at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(Unknown Source)
at javafx.animation.Animation.impl_timePulse(Unknown Source)
at javafx.animation.Animation$1.lambda$timePulse$26(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.Animation$1.timePulse(Unknown Source)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Unknown Source)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$405(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
我在时钟类中使用了时间轴。我想这就是问题所在。我可以在不使用时间线的情况下构建工作时钟吗?
最佳答案
似乎在 Java 1.8.0_60 中,类 Alert 的 showAndWait() 方法不能在动画期间调用(基本上这就是异常所说的)。问题是我使用时间线来检查系统时间,并在达到特定时间时显示警报。为了解决问题,我改变了
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
alarm.stop();
try {
func.call();
} catch (Exception e) {
e.printStackTrace();
}
}
为此:
alert.showingProperty().addListener((observable,oldValue,newValue)->{
if (!newValue){
alarm.stop();
try {
func.call();
} catch (Exception e) {
e.printStackTrace();
}
}
});
因此,它不会等待一些输入,而是简单地监听 showing 属性的变化。如果您使用其他带有更多按钮的警报,则不知道如何解决此问题。
关于JavaFX 警报未显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425660/