简体   繁体   English

TornadoFX 加载多个 FXML 文件

[英]TornadoFX load multiple FXML files

I am working on a UI using Kotlin and TornadoFX where there is a "Main" Part, and depending on which button I press the "Main" Part of the View (currently an AnchorPane) should change like this:我正在使用 Kotlin 和 TornadoFX 开发一个 UI,其中有一个“主要”部分,并且根据我按下视图的“主要”部分(当前是 AnchorPane)的哪个按钮应该改变如下:

启动时主视图的内容

按下按钮后的内容

I tried loading in 2 FXML documents and adding the Content to my Main-Anchorpane like this:我尝试加载 2 个 FXML 文档并将内容添加到我的 Main-Anchorpane,如下所示:

override val root: BorderPane by fxml("/views/MainView.fxml")
val contentContainer: AnchorPane by fxid("contentContainer")

val contentBtn1: AnchorPane by fxml("/views/MainViewProject.fxml")

val btnProject: JFXButton by fxid("btnProject")

init {
    btnProject.setOnAction {
        contentContainer += contentBtn1
    }
}

but I get the following error:但我收到以下错误:

javafx.fxml.LoadException: Controller value already specified.

I solved this by creating a new View for the second FXML file:我通过为第二个 FXML 文件创建一个新视图解决了这个问题:

class MainProjectView : View("My View") {
   override val root: HBox by fxml()
}

Then injecting the View in my MainViewClass and adding it there:然后在我的 MainViewClass 中注入 View 并将其添加到那里:

    val mainProjectView: MainProjectView by inject()
    
    init {
        mainPane += mainProjectView
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM