无法使用 TornadoFX 使节点在 StackPane 中居中

Can't get node to center in a StackPane with TornadoFX

我正在尝试制作一个简单的视图,其中我有一个背景节点(现在,我在这里有一个文本节点,但最终节点将是一个 SVG 路径)一个VBox。在我继续实施 VBox 之前,我试图让我的背景节点在根 StackPane 中居中,但我似乎无法让我的节点正确居中。

这是我的 classes:

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.. 如果相关的话。

class UIStyles : Stylesheet() {
  companion object {
    val headerFontStyle by cssclass()
    private val headerFontColor = Color.BLACK
  }

  init {
    headerFontStyle {
      fontSize = 48.px
    }
  }
}

是否有其他方法可以让我将节点置于 StackPane 的中心?我也在 InputView 初始化函数中尝试了 root.alignment = Pos.CENTER,但无济于事。

我觉得我在做一些愚蠢的事情..有什么帮助吗?谢谢!

尝试将 super.start() 调用放在 minWidth/Height 设置调用之前。

我们已经在 Slack 上讨论过这个问题,我们认为 Linux 和 Mac/Windows 之间可能存在 JavaFX 差异,因为您的代码在 Mac/Windows 上开箱即用但失败了在 Linux.

当您在 minWidth/minHeight 配置之前放置 super.start() 调用时,场景已经为您创建,因此我认为这就是原因。

默认情况下,StackPane 将其子项居中,因此不需要 stackpaneConstraints 配置。如果您也使用构建器来定义堆栈窗格,您的代码也会看起来更清晰:

class InputView : View() {
    override val root = stackpane {
        text("I wish to be centered")
    }
}