tornadofx - 透明 View

标签 tornadofx

我想创建一个具有部分透明背景的 View (舞台、窗口)。我有一张包含 Alpha channel 的图像

an image containing alpha channel

我在JavaFx中使用了这种场景,我必须将场景填充设置为空,并将根节点背景颜色设置为透明。我对 TornadoFX 进行了同样的尝试:

class NextRoundView : View("Következő kör") {

    override val root = vbox {
        style {
            backgroundColor = multi(Color.TRANSPARENT)
            backgroundImage = multi(URI.create("/common/rope-bg-500x300.png"))
            backgroundRepeat = multi(BackgroundRepeat.NO_REPEAT 
                                  to BackgroundRepeat.NO_REPEAT)
        }
        prefWidth = 500.0
        prefHeight = 300.0

        spacing = 20.0
        padding = insets(50, 20)
        text("A text") {
            font = Font.font(40.0)
            alignment = Pos.CENTER
        }

        button("OK")
        {
            font = Font.font(20.0)
            action {
                close()
            }
        }
        sceneProperty().addListener{ _,_,n ->
            n.fill = null
        }
    }

}

我这样调用 View :

NextRoundView().apply { 
    openModal(stageStyle = StageStyle.TRANSPARENT, block = true) 
}

但是,舞台还是有背景的:

enter image description here

我错过了什么?

最佳答案

您犯了一些错误,导致了这种情况。首先,绝对不能手动实例化 UICompoenents(View、Fragment)。这样做会让他们错过重要的生命周期回调。一个重要的回调是 onDock,它是操作指定场景的完美位置。改变这两个问题并清理一些语法导致了这段代码,它成功地使背景透明:

class MyApp : App(MyView::class)

class MyView : View() {
    override val root = stackpane {
        button("open").action {
            find<NextRoundView>().openModal(stageStyle = StageStyle.TRANSPARENT, block = true)
        }
    }
}

class NextRoundView : View("Következő kör") {
    override val root = vbox {
        style {
            backgroundColor += Color.TRANSPARENT
            backgroundImage += URI.create("/common/rope-bg-500x300.png")
            backgroundRepeat += BackgroundRepeat.NO_REPEAT to BackgroundRepeat.NO_REPEAT
        }
        prefWidth = 500.0
        prefHeight = 300.0

        spacing = 20.0
        padding = insets(50, 20)
        text("A text") {
            font = Font.font(40.0)
            alignment = Pos.CENTER
        }

        button("OK") {
            font = Font.font(20.0)
            action {
                close()
            }
        }
    }

    override fun onDock() {
        currentStage?.scene?.fill = null
    }
}

以下是已实现更改的应用程序的屏幕截图:

enter image description here

关于tornadofx - 透明 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418919/

相关文章:

datepicker - 在 Kotlin TornadoFX 上获取日期选择器日期

java - tornadoFX 切换按钮没有文本属性

kotlin - Kotlin | Tornadofx:如何在另一个fxml屏幕上单击鼠标时打开新的fxml屏幕

javafx - Tornadofx - 如何在每个实例上将参数传递给 Fragment

kotlin - 选择组合框中的项目时调用函数 TornadoFX

javafx - 在 tornadofx 中创建自定义 UI 组件的最佳实践是什么?

kotlin - 为什么我的 tornadoFX ObservableList 没有收到更新?

javafx - TornadoFX:在ValidationContext中比较2个表单值

listview - 与kotlin和tornadofx在listview上无休止地滚动

kotlin - doubleBinding 没有效果