首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Pane中加载fxml文件?

如何在Pane中加载fxml文件?
EN

Stack Overflow用户
提问于 2015-11-17 02:26:22
回答 3查看 27.4K关注 0票数 17

如果我们有一个Stage,那么Scene包含2个Panes,第一个Pane包含Button,第二个Pane是空的,我们能在这个第二个Pane中加载其他的fxml文件吗?

代码语言:javascript
复制
fxml1: VBox
               |_Pane1-->Button
               |_Pane2
///////////////
fxml2: Pane--> Welcome to fxml 2
"when we click the button load the fxml2 inside Pane2 of fxml1"

然后点击后

====I在尝试后终于发现了它的作用!====Thank,伙计们

代码语言:javascript
复制
@FXML Pane secPane;
public void loadFxml (ActionEvent event) {
Pane newLoadedPane =        FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
secPane.getChildren().add(newLoadedPane); 
}  
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-17 22:41:07

经过尝试,我终于找到了这个工作!

代码语言:javascript
复制
@FXML Pane secPane;
public void loadFxml (ActionEvent event)  {
  Pane newLoadedPane =  FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
  secPane.getChildren().add(newLoadedPane);
}
票数 25
EN

Stack Overflow用户

发布于 2015-11-17 13:00:59

仅仅替换控制器类中的字段不会改变场景图。

secPane只是对场景图中一个节点的引用。

如果secPane只是一个占位符,您可以在父级的子列表中替换它:

代码语言:javascript
复制
public void loadFxml (ActionEvent event) {
    // load new pane
    Pane newPane = FXMLLoader.load(getClass().getResource("/application/Login2.fxml"));

    // get children of parent of secPane (the VBox)
    List<Node> parentChildren = ((Pane)secPane.getParent()).getChildren();

    // replace the child that contained the old secPane
    parentChildren.set(parentChildren.indexOf(secPane), newPane);

    // store the new pane in the secPane field to allow replacing it the same way later
    secPane = newPane;
}

当然,这假定getClass().getResource("/application/Login2.fxml")生成正确的资源,并且不返回null (如果没有指定名称的资源可用,就会发生这种情况)。

票数 4
EN

Stack Overflow用户

发布于 2015-11-17 02:41:25

您可以实现如下内容:

代码语言:javascript
复制
 public void start(Stage primaryStage) throws IOException {
            primaryStage.setTitle("Title");
            primaryStage.setScene(createScene(loadMainPane("path_of_your_fxml")));
            primaryStage.show();

    }

    private Pane loadMainPane(String path) throws IOException {
        FXMLLoader loader = new FXMLLoader();

        Pane mainPane = (Pane) loader.load(
                getClass().getResourceAsStream(path));

        return mainPane;
    }


    private Scene createScene(Pane mainPane) {
        Scene scene = new Scene(mainPane);
      return scene;
    }

然后,您可以创建一个单独的类调用导航来存储所有的fxml路径:

代码语言:javascript
复制
public class Navigator {

private final String P1;
private final String P2;
//then you can implement getters...
public String getP1() {
    return P1;
}

public String getP2() {
    return p2;
}

private static FxmlController Controller;

    public static void loadPane(String fxml) {
    try {
        FxmlController.setPane(
                (Node) FXMLLoader.load(Navigator.class.getResource(fxml)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public Navigator() throws IOException {
    this.P1 = "p1.fxml";
    this.P2 = "p2.fxml";}

然后,您可以在按钮中加载窗格,如下所示:

代码语言:javascript
复制
@FXML
    private void btnAction(ActionEvent event) throws IOException {
    Navigator.load(new Navigator().getP1());
    ..

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33748127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档