我想创建一个具有部分透明背景的 View (舞台、窗口)。我有一张包含 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)
}
但是,舞台还是有背景的:
我错过了什么?
最佳答案
您犯了一些错误,导致了这种情况。首先,绝对不能手动实例化 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
}
}
以下是已实现更改的应用程序的屏幕截图:
关于tornadofx - 透明 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418919/