在TornadoFX中设置PrimaryStage或Scene属性的方法
我是新的tornadoFX,我不知道如何设置PrimaryStage或场景的属性,如场景的高度或宽度或PrimaryStage模态。 请帮帮我。
UPDATE
我想设置场景的高度和宽度,看这个例子:
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")) } }
如果您不想让主视图指定初始场景大小,则可以覆盖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: View) = Scene(view.root, 800.0, 600.0)
最终结果将是相同的,所以你可以保留在第一个例子中的代码。
你一定要看看TornadoFX指南 。 这是TornadoFX入门的好资源。
要回答您的问题,您可以在视图的根目录中设置大小。 这应该做你想做的(使用TornadoFX的构建器模式):
class Main : App() { override val primaryView = MyView::class } class MyView : View() { override val root = VBox() init { with (root) { prefWidth = 800.0 prefHeight = 600.0 label("My label") } } }
另一个选择是使用类型安全的样式表 :
class Main : App() { override val primaryView = MyView::class init { importStylesheet(Style::class) } } class MyView : View() { override val root = VBox() init { with (root) { label("My label") } } } class Style : Stylesheet() { init { root { prefHeight = 600.px prefWidth = 800.px } } }
类型安全的样式表的好处是你可以使用不同的单位(你可以简单的说prefHeight = 10.cm
或prefWidth = 5.inches
)。 它基本上可以做任何CSS可以做的事情,但更方便,更强大,并且(顾名思义)类型安全。
免责声明:我参与设计和构建TornadoFX的类型安全样式表系统。