JavaFX window 在左上角打开然后跳到中间
JavaFX window opens in top left corner then jumps to center
我正在 JavaFX 中开发一个程序(更具体地说是 FornadoFX,因为我是用 Kotlin 编写的)。我注意到,当 main window 打开时,它最初在左上角,然后跳到中间。
这是我启动应用程序的方式: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 答案。
然而无论他们是否在那里 window 总是在左上角打开然后跳转到设置位置,而不是首先显示在那里。
编辑:
这是该错误的最小工作示例:
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
我正在 JavaFX 中开发一个程序(更具体地说是 FornadoFX,因为我是用 Kotlin 编写的)。我注意到,当 main window 打开时,它最初在左上角,然后跳到中间。
这是我启动应用程序的方式: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 答案。
然而无论他们是否在那里 window 总是在左上角打开然后跳转到设置位置,而不是首先显示在那里。
编辑:
这是该错误的最小工作示例:
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