在主fxml文件中加载fxml文件 [英] Loading fxml files inside a main fxml file

查看:260
本文介绍了在主fxml文件中加载fxml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a main fxml file with several menus. When the user clicks certain buttons, the content changes and these contents are the seperate fxml files which loads on the main screen. The entire scene doesn't change but only a part of the scene changes instead. Here is a simple prototype of the application which I want to make![enter image description here][1]

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

推荐答案

试试这个..

    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

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

这篇关于在主fxml文件中加载fxml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆