ScalaFXJavaFX 的 Scala 绑定

联合创作 · 2023-09-28

ScalaFX 是用 Scala 语言编写的UI DSL,位于JavaFX 2和JavaFX 8之上。每个ScalaFX应用程序也是有效的Scala应用程序。它支持与Java的完全互操作性,并且可以在支持Java虚拟机(JVM)和JavaFX2或JavaFX8的任何地方运行。

ScalaFX使用简单的分层模式创建新对象并构建场景图。

主要特性如下:

1)对程序员友好的层次结构式的对象创建语法。

stage = new Stage {
  title.value = "Hello Stage"
  width = 600
  height = 450
  scene = new Scene {
    fill = Color.LIGHTGREEN
    content = new Rectangle {
      x = 25
      y = 40
      width = 100
      height = 100
      fill <== when (hover) then Color.GREEN otherwise Color.RED
    }
  }
}

2)易于理解的属性绑定(Binding)语法。

height <== rect1.height + rect2.height
width <== max(rect1.width, rect2.width, rect3.width)
color <== when (hover) then Color.GREEN otherwise Color.RED
text <== when (rect.hover || circle.hover && !disabled) then textField.text + " is enabled" otherwise "disabled"
rect.hover onInvalidate {
  needsRepaint = true
}

3)精心设计的动画语法。

Seq(
  at(0 s) {circle.centerX -> random * 800},
  at(0 s) {circle.centerY -> random * 600},
  at(40 s) {circle.centerX -> random * 800},
  at(40 s) {circle.centerY -> random * 600}
)

4)类型安全的API。

5)无缝的JavaFX/ScalaFX互操作。

浏览 3
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报