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

时间:2016-11-19 14:54:04

标签: fxml

我的问题与这两个问题大致相同:

- JavaFX how to inject new FXML content to current Scene

- set the content of a anchorPane with fxml file

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

我该如何实现?

1 个答案:

答案 0 :(得分:1)

计划入口点:

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);
}

}

MainScreenController

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()来更改屏幕。

相关问题