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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33748127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:05:46  来源:igfitidea点击:

How to load fxml file inside Pane?

javajavafxjavafx-2fxml

提问by Hussein Saied

enter image description here

在此处输入图片说明

If we have a Stagethen Sceneincludes 2 Panes the 1st Panecontains Buttonand the 2nd Paneis empty could we load other fxml file inside this 2nd Pane?

如果我们有一个StagethenScene包含 2 Panes,第一个Pane包含Button而第二个Pane为空,我们可以在第二个中加载其他 fxml 文件Pane吗?

fxml1: VBox
               |_Pane1-->Button
               |_Pane2
///////////////
fxml2: Pane--> Welcome to fxml 2
"when we click the button load the fxml2 inside Pane2 of fxml1"

Then after click

然后点击后

enter image description here

在此处输入图片说明



====I finally found this works after trying !====Thank you guys

====我终于在尝试后找到了这个!====谢谢你们

@FXML Pane secPane;
public void loadFxml (ActionEvent event) {
Pane newLoadedPane =        FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
secPane.getChildren().add(newLoadedPane); 
}  

回答by Hussein Saied

I finally found this works after trying !

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

@FXML Pane secPane;
public void loadFxml (ActionEvent event)  {
  Pane newLoadedPane =  FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
  secPane.getChildren().add(newLoadedPane);
}

回答by fabian

Just replacing the field in your controller class won't change the scene graph.

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

secPaneis just a reference to a node in the scene graph.

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

If secPaneis just a placeholder, you could replace it in the parent's child list:

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

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

This assumes of course, that getClass().getResource("/application/Login2.fxml")yields the correct resource and does not return null(which happens if no resource with the given name is available)

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

回答by Madushan Perera

You can implement something like this :

你可以实现这样的东西:

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

Then you can create a separate class call Navigationto store all your fxml paths:

然后你可以创建一个单独的类调用Navigation来存储你所有的 fxml 路径:

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

Then you can load your pane in your button like below:

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

@FXML
    private void btnAction(ActionEvent event) throws IOException {
    Navigator.load(new Navigator().getP1());
    ..

.

.