【发布时间】:2019-08-09 08:02:22
【问题描述】:
我正在用 JavaFX 开发一个程序(更具体地说是 FornadoFX,因为我是用 Kotlin 编写的)。我注意到的是,当主窗口最初在左上角打开时,它会跳到中心。
这就是我启动应用程序的方式:launch<MainWindowClass>(args)
这是我的start 方法:
override fun start(stage: Stage) {
with(stage){
minWidth = 600.0
minHeight = 250.0
//Making it appear in the center
val screenBounds = Screen.getPrimary().visualBounds
x = screenBounds.width / 2 - minWidth / 2
y = screenBounds.height / 2 - minHeight / 2
scene = Scene(Group(), minWidth, minHeight)
super.start(this)
}
}
中心部分(从val screenBounds... 到scene = ... 的行)基于this 答案。
但是无论他们是否在那里,窗口总是在左上角打开,然后跳转到设置位置,而不是首先显示在那里。
编辑:
这是该错误的最小工作示例:
import javafx.scene.Group
import javafx.scene.Scene
import javafx.stage.Screen
import javafx.stage.Stage
import tornadofx.*
class MainWindow: App(MainView::class) {
class MainView: View() {
override val root = label("A window")
}
companion object {
@JvmStatic fun main(args: Array<String>){
launch<MainWindow>(args)
}
}
override fun start(stage: Stage) {
with(stage){
minWidth = 600.0
minHeight = 250.0
val screenBounds = Screen.getPrimary().visualBounds
x = screenBounds.width / 2 - minWidth / 2
y = screenBounds.height / 2 - minHeight / 2
scene = Scene(Group(), minWidth, minHeight)
super.start(this)
}
}
}
还有一些关于系统的信息: Java 版本:11 操作系统:Ubuntu 18.04 LTS
【问题讨论】:
-
请发布一个表现出这种行为的完整可运行应用程序:)
-
@EdvinSyse 我做到了。如您所见,
start方法之外没有太多事情发生 -
看到这个answer,不是同一个问题,但有相同的根本原因。
-
@JoséPereda 谢谢。这确实解决了问题。谢谢 :) 您可以将其发布在评论中,以便我将其标记为解决方案
-
好的,我已经添加它作为答案,这样做我注意到问题可能已经解决了,所以也许你可以试试 JavaFX 13-ea+11?
标签: java javafx kotlin tornadofx