在主fxml文件中加载fxml文件

时间:2014-01-21 07:39:42

标签: java javafx fxml

我有一个包含多个菜单的主fxml文件。当用户单击某些按钮时,内容会发生变化,这些内容是在主屏幕上加载的单独fxml文件。整个场景不会改变,但只有一部分场景发生了变化。这是我想要制作的应用程序的简单原型![在此处输入图像描述] [1]

https://www.dropbox.com/s/3nildyt004v32vc/fxml%20application.png

1 个答案:

答案 0 :(得分:0)

试试这个..

    AnchorPane pane; // pane where fxml was showing
    menu1.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent t) {
        openNewWindow("m1.fxml");

    }
    });
    menu2.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent t) {            
        openNewWindow("m2.fxml");

    }});
    menu3.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent t) {            
        openNewWindow("m3.fxml");

    }
    }); 

功能 openNewWindow

public void openNewWindow(String FXMLFile)
 {
     //ChildNode child;
    try {                    
        URL url = getClass().getResource(FXMLFile);
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(url);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        AnchorPane page = (AnchorPane) fxmlLoader.load(url.openStream()); 

        Pane.getChildren().clear();///name of pane where you want to put the fxml
        Pane.getChildren().add(page);
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
 }