Scalafx。启动计时器警报

时间:2017-12-09 20:44:20

标签: scala timer alert scalafx

我有一个包含一些信息的主窗口。我无法通过计时器启动警报。我需要每隔10秒显示一次警报(使用工作主窗口),然后通过警报按钮更改主窗口中标签的文本。 我有这段代码,但它不起作用:

object main extends JFXApp {
 stage = new JFXApp.PrimaryStage() {
   scene = new Scene {
    val MyLabel = new Label("SomeText")
    root = new VBox {
        children = MyLabel
     }
   }
 val ButtonTypeOne = new ButtonType("Change the text")
 val ButtonTypeTwo = new ButtonType("No")

  val alert1 = new Alert(AlertType.Warning) {
        initOwner(stage)
        title = "Warning!!!"
        headerText = "Header"
        contentText = "Do you need to change the text?"
        buttonTypes = Seq(ButtonTypeOne, ButtonTypeTwo, ButtonType.Cancel)
      }
    val result = alert1.showAndWait()
    val timerA = new PauseTransition(Duration(5000))
              timerA.onFinished = { _ =>
                result match {
                  case Some(ButtonTypeOne) => /*Here I need to change text in MyLabel */
                  case Some(ButtonTypeTwo) => None
                  case _ => None
                 }
               timerA.playFromStart()
              }
    timerA.play
 }
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些不同的问题:

  1. 只显示一次警报,无论计时器发生了什么。
  2. 对计时器中警报结果作出反应的代码始终会查看相同的初始结果。
  3. 不幸的是,即使您在计时器的动画更新事件中移动val result = alert1.showAndWait JavaFX 也会在此类例程中调用showAndWait非法。
  4. 解决方案是为onHidden创建一个alert1事件处理程序,它会对其进行响应。然后,用于关闭对话框的按钮类型存储在alert1.result中,因此您可以使用它来确定要采取的操作。

    我添加了StringProperty来帮助更改标签值。以下示例应该是您想要实现的目标的良好起点......

    import scalafx.Includes._
    import scalafx.animation.PauseTransition
    import scalafx.application.JFXApp
    import scalafx.application.JFXApp.PrimaryStage
    import scalafx.beans.property.StringProperty
    import scalafx.scene.Scene
    import scalafx.scene.layout.VBox
    import scalafx.scene.control.{Alert, ButtonType, Label}
    import scalafx.scene.control.Alert.AlertType
    import scalafx.util.Duration
    
    object main extends JFXApp {
    
      // String property for the text in myLabel.
      val strProp = StringProperty("Some Text")
    
      // Declare this so that it's accessible from timerA.onFinished.
      val myLabel = new Label {
    
        // Bind the text property to the value of strProp.
        // When strProp's value changes, so does the label's text.
        text <== strProp
      }
    
      stage = new PrimaryStage() {
        scene = new Scene {
          root = new VBox {
            children = myLabel
          }
        }
      }
    
      // Custom buttons.
      val ButtonTypeOne = new ButtonType("Change the text")
      val ButtonTypeTwo = new ButtonType("No")
    
      // Create the timer. This goes off after 5,000ms (5 seconds) - after play is called.
      val timerA = new PauseTransition(Duration(5000))
    
      // Alert dialog.
      // Note: JavaFX forbids use of showAndWait within animation processing, so we must use
      // an onHidden event instead.
      val alert1 = new Alert(AlertType.Warning) {
        initOwner(stage)
        title = "Warning!!!"
        headerText = "Header"
        contentText = "Do you need to change the text?"
        buttonTypes = Seq(ButtonTypeOne, ButtonTypeTwo, ButtonType.Cancel)
      }
    
      // React to the dialog being closed.
      alert1.onHidden = {_ =>
        alert1.result.value match {
    
          // If button type one, change the property value.
          // Note alert1.result.value is a JavaFX ButtonType, so use .delegate for the match.
          case ButtonTypeOne.delegate => strProp.value = "Changed!"
    
          // Otherwise, do nothing.
          case _ =>
        }
    
        // Start the timer once more.
        // This is going to be a very annoying app! ;-)
        timerA.playFromStart()
      }
    
      // When the timer goes off, show the alert.
      timerA.onFinished = {_ =>
        alert1.show()
      }
    
      // Start the timer for the first time.
      timerA.play
    }