我正在尝试制作一个简单的 View ,其中在 VBox
后面有一个背景节点(目前,我在这里有一个文本节点,但最终节点将是 SVG 路径) 。我试图让我的背景节点在根 StackPane
内居中在我继续实现 VBox
之前,但我似乎无法让我的节点正确居中。
这是我的类(class):
class UserInputUI : App(InputView::class, UIStyles::class) {
init {
reloadStylesheetsOnFocus()
}
override fun start(stage: Stage) {
stage.minWidth = 1024.0
stage.minHeight = 768.0
super.start(stage)
}
}
class InputView : View() {
override val root = StackPane()
init {
text("I wish to be centered") {
stackpaneConstraints {
alignment = Pos.CENTER
addClass(UIStyles.headerFontStyle)
}
}
}
}
还有我的UIStyles
类..如果相关的话。
class UIStyles : Stylesheet() {
companion object {
val headerFontStyle by cssclass()
private val headerFontColor = Color.BLACK
}
init {
headerFontStyle {
fontSize = 48.px
}
}
}
有没有一种不同的方法可以将节点居中于 StackPane
的中间?我试过root.alignment = Pos.CENTER
在 InputView
init 函数也有,但无济于事。
我觉得我在做一些愚蠢的事情......有什么帮助吗?谢谢!
最佳答案
我们已经在 Slack 上讨论了这个问题,我们相信 Linux 和 Mac/Windows 之间可能存在 JavaFX 差异,因为您的代码在 Mac/Windows 上开箱即用,但在 Linux 上失败。
当您在 minWidth/minHeight
配置之前调用 super.start()
时,场景已经为您创建好了,所以我相信原因。
默认情况下,StackPane 会将其子级居中,因此不需要 stackpaneConstraints
配置。如果您也使用构建器来定义堆栈 Pane ,您的代码也会看起来更干净:
class InputView : View() {
override val root = stackpane {
text("I wish to be centered")
}
}
关于javafx - 无法使用 TornadoFX 将节点置于 StackPane 中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44510083/