使用另一个 FXML 文件的窗格设置窗格的内容

Set the content of a pane with the pane of another FXML file

我的问题和这两个差不多:

-

-set the content of a anchorPane with fxml file

我无法使用这些答案,因为我不理解它们。 我有两个(后来更多)FXML 文件,其中一个用作 'main',它有一个窗格,应将另一个 FXML 文件的内容添加到其中。

我应该如何实施?

程序入口点:

public class App extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
    Parent startScreen
 = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
    Scene scene = new Scene(startScreen);
    primaryStage.setScene(scene);
    primaryStage.show();

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

主屏幕控制器

public class MainScreenController implements Initializable{

private static AnchorPane contentBox;

@FXML
private AnchorPane paneContent;

public MainScreenController() throws IOException {
    this.paneContent = FXMLLoader.load(getClass().getResource("Home.fxml"));

}

@Override
public void initialize(URL url, ResourceBundle rb) {
     MainScreenController.contentBox = this.paneContent;
}  

public static AnchorPane getContentBox(){
    return MainScreenController.contentBox;
}

}

然后 MainScreen.fxml 需要将 MainScreenController 作为控制器,还需要包含一个带有 fx:id paneContent 的 AnchorPane。 然后,您可以从程序的任何位置调用 getContentBox() 并使用 .set() 来更改屏幕。