从FXML添加锚定窗格并将其交换到当前场景

时间:2017-04-05 11:12:49

标签: javafx javafx-2 javafx-8

我一直在处理我正在处理的项目的一些问题。我希望将我的fxml内容加载到现有的anchorpane中,并在" next"单击按钮,我也希望有"返回"按钮将anchorpane设置为上一个fxml文件。

我遇到的问题是我只有一次工作。如果单击下一步,则显示正确的fxml内容,但是如果按下后退,则它会正常运行一次,第二次显示的窗口不包含任何内容:

@FXML
AnchorPane scenePane, allContent;

@FXML
public void handleNextAction() {
    try {
        scenePane.getChildren().clear();
        AnchorPane newvalscene = (AnchorPane) FXMLLoader.load(getClass().getResource("ProjectDetails.fxml"));
        scenePane.getChildren().setAll(newvalscene.getChildren());
    } catch (IOException ex) {
        Logger.getLogger(NewProjectController.class.getName()).log(Level.SEVERE, null, ex);
    }
}


@FXML
public void handleBackAction() {
    try {
       // allContent.getChildren().clear();
        AnchorPane newvalscene = (AnchorPane) FXMLLoader.load(getClass().getResource("newProjectWindow.fxml"));
        allContent.getChildren().setAll(newvalscene.getChildren());
    } catch (IOException ex) {
        Logger.getLogger(NewProjectController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我已经尝试清除了anchorpanes的内容,但这只会导致anchorpane为空,第二次按下后退按钮时没有从FXML加载内容。

注意我试过调试这个但是因为没有实际的错误很难跟踪,netbeans似乎不会向我展示变量newvalscene的内容,其中包含要渲染的所有节点,但是println显示它确实包含了Vbox和孩子们,我希望能够出现。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

为什么所有这些SIr都有原因?

scenePane.getChildren().clear();
scenePane.getChildren().setAll(newvalscene.getChildren());//why replace?

allContent.getChildren().setAll(newvalscene.getChildren());//why replace ?

请试试这个

如果你真的有这个

AnchorPane scenePane, allContent;

和scenePane是您的父级或根,您可以嵌套您的项目,如

scenePane.getCHildren().add(child);

所以与您的代码有关

AnchorPane nextvalscene,backvalscene;

public void handleNextAction() {
try {
    //scenePane.getChildren().clear(); remove this
    if(nextvalscene == null){
    nextvalscene = (AnchorPane) FXMLLoader.load(getClass().getResource("ProjectDetails.fxml"));
    scenePane.getChildren().add(newvalscene);//change setAll to add and use the child itself
    AnchorPane.setLeftNode(newvalscene, 0.0);//set constraint for your newvalscene AnchorPane
    //do that for the rest esp top
    }
    nextvalscene.toFront();
} catch (IOException ex) {
    Logger.getLogger(NewProjectController.class.getName()).log(Level.SEVERE, null, ex);
}
}

对handleBackAction执行相同操作

逻辑是你的父或根场景是AnchorPane它可以包含子节点,所以不需要替换它的所有子节点,你可以通过布局参数的约束遮挡它们,并改变它的重新排序。

除非你有理由将你的Anchorpane儿童复制到你的场景儿童名单,我想我错过了

希望有所帮助

相关问题