JavaFX警报不显示?

问题描述:

我没有太多的信息,但也许有人面临同样的问题。我为一些朋友编写了一个应用程序,它包含一些警报对话。问题是警报没有显示在它测试过的一台电脑上(但是在我的电脑上,它确实!)。我有Windows 10和Java 1.8.0_51,测试系统有Windows 7和Java 1.8.0_60。JavaFX警报不显示?

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

因此,而不是等待一些输入,它只是监听在显示属性的更改。不知道如何解决这个问题,如果你使用更多的按钮,但其他警报。