在 TornadoFX 中设置 PrimaryStage 或 Scene 属性的方式

Way of setting PrimaryStage or Scene properties in TornadoFX

我是 tornadoFX 的新手,我不知道如何设置 PrimaryStage 或场景属性,例如场景高度或宽度或 PrimaryStage 模态。 请帮我。

更新

我想设置场景的高度和宽度,看这个例子:

dependencies {
compile 'no.tornado:tornadofx:1.5.2'
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.3"
}


import javafx.scene.control.Label
import javafx.scene.layout.VBox
import tornadofx.App
import tornadofx.FX
import tornadofx.View

class Main : App() {
   override val primaryView = MyView::class

   init {
      // this two lines have error ( Val cannot be reassigned. )
      FX.primaryStage.scene.height = 600.0
      FX.primaryStage.scene.width = 800.0
      // or this line causes this exception ( java.lang.NoSuchMethodException )
      FX.primaryStage.isResizable = false
   }

}

class MyView : View() {
   override val root = VBox()

   init {
      root.children.add(Label("My label"))
   }
}

您绝对应该查看 TornadoFX Guide。这是开始使用 TornadoFX 的重要资源。

要回答您的问题,您可以在视图的根目录中设置大小。这应该做你想做的(使用 TornadoFX 的构建器模式):

class Main : App(MyView::class)

class MyView : View() {
    override val root = vbox {
        prefWidth = 800.0
        prefHeight = 600.0

        label("My label")
    }
}

另一种选择是使用 type safe stylesheets:

class Main : App(MyView::class, Style::class)

class MyView : View() {
    override val root = vbox {
        label("My label")
    }
}

class Style : Stylesheet() {
    init {
        root {
            prefHeight = 600.px
            prefWidth = 800.px
        }
    }
}

类型安全样式表的优点是您可以使用不同的单位(您可以简单地设置 prefHeight = 10.cmprefWidth = 5.inches)。它基本上可以做任何 CSS 可以做的事情,但更方便、更强大,而且(顾名思义)类型安全。

免责声明:我参与了 TornadoFX 类型安全样式表系统的设计和构建。

如果您不想让主视图决定初始场景大小,您可以覆盖 App.start 并配置主舞台的尺寸,这将再次决定场景的尺寸:

override fun start(stage: Stage) {
    super.start(stage)
    stage.width = 800.0
    stage.height = 600.0
}

为了使这更简单,TornadoFX 1.5.3 中将有一个功能可以让您自己为主视图创建场景:

override fun createPrimaryScene(view: UIComponent) = Scene(view.root, 800.0, 600.0)

最终结果是一样的,所以您可以保留第一个示例中的代码。