我试图使用JavaFX编写自己的确认对话框,当按下 okButton 时会调用lambda表达式。
ConfirmDialog.show("Delete selected items?") {
// actions...
}
此实现中的问题是永远不会调用来自actionByConfirm
函数的show()
。请告诉我,如何纠正它?
object ConfirmDialog : UnborderedStage() {
override val pathToScene: String
get() = "/module/main/confirm/confirm.fxml"
val title: Label
val okButton: Button
val cancelButton: Button
var actionByConfirm: (() -> Unit)? = null
init {
title = scene.lookup("#title") as Label
okButton = scene.lookup("#buttonOk") as Button
cancelButton = scene.lookup("#buttonCancel") as Button
okButton.setOnMouseClicked {
actionByConfirm?.invoke()
hide()
}
cancelButton.setOnMouseClicked { hide() }
}
fun show(title: String, actionByConfirm: () -> Unit) {
this.title.text = title
this.actionByConfirm = actionByConfirm
show()
}
}
* UnborderedStage是同一个舞台,但是通过聚焦 StageStyle.UNDECORATED 和关闭动作。