简体   繁体   English

JavaFX警报未显示?

[英]JavaFX Alerts not showing?

I don't have quite much information, but perhaps someone was facing the same problem. 我没有很多信息,但是也许有人正面临着同样的问题。 I wrote an application for some friends, and it includes some Alert Dialogues. 我为一些朋友编写了一个应用程序,其中包括一些警报对话。 The problem is that the alerts are not showing on one of the pcs it was tested (but on my pc, it does!). 问题在于,警报未在经过测试的其中一台计算机上显示(但在我的计算机上却显示了!)。 I have Windows 10 and Java 1.8.0_51, the testing system had Windows 7 and Java 1.8.0_60. 我有Windows 10和Java 1.8.0_51,测试系统有Windows 7和Java 1.8.0_60。

I styled it with css. 我用CSS设置了样式。 This is how I implemented it (alarm is just an audioclip): 这就是我的实现方式(警报只是音频剪辑):

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();
        }
    }

Edit: I updated to 1.8.0_60 too, and the dialog isn't showing on my system either! 编辑:我也更新到1.8.0_60,并且该对话框也未显示在我的系统上! This is the exception I get: 这是我得到的例外:

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)

I use a timeline in the class Clock.. I guess that's the problem. 我在Clock类中使用了时间轴。我想这就是问题所在。 can I build a working clock without using timeline? 不使用时间轴就能建立工作时钟吗?

It seemes like in Java 1.8.0_60, the showAndWait() method of the class Alert can't be called during an animation (basically that's what the exception says). 似乎在Java 1.8.0_60中,不能在动画过程中调用Alert类的showAndWait()方法(基本上,这就是例外情况)。 The problem was that I used a timeline to check the system time, and showed an alarm when a certain time is reached. 问题是我使用时间线检查系统时间,并在达到特定时间时显示警报。 To solve the problem, I changed 为了解决这个问题,我改变了

Optional<ButtonType> result = alert.showAndWait();
            if (result.get() == ButtonType.OK) {
                alarm.stop();
                try {
                    func.call();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

to this: 对此:

alert.showingProperty().addListener((observable,oldValue,newValue)->{
                if (!newValue){
                    alarm.stop();
                    try {
                        func.call();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

So instead of waiting for some input, it simply listens to changes in the showing property. 因此,它无需等待输入,而只是侦听showing属性中的更改。 Don't know how to fix this if you use other alerts with more buttons though. 但是,如果您通过其他按钮使用其他警报,则不知道如何解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM