如何在JavaFx中更改视图的一部分

如何在JavaFx中更改视图的一部分,javafx,javafx-8,Javafx,Javafx 8,我有一个视图(水平分割的窗格)。让我们把这两部分命名为lowerAnchorPane和upperAnchorPane。我有一个按钮。我希望应用程序在按下该按钮后将lowerAnchorPane切换到FXML文件中创建的另一个AnchorPane。我想在showPatients()函数中实现这一点,但它似乎不起作用,您能给我建议一些解决方案吗?我不知道其他加载fxml文件的方法 public class MainController { private Patient model;

我有一个视图(水平分割的窗格)。让我们把这两部分命名为lowerAnchorPane和upperAnchorPane。我有一个按钮。我希望应用程序在按下该按钮后将lowerAnchorPane切换到FXML文件中创建的另一个AnchorPane。我想在showPatients()函数中实现这一点,但它似乎不起作用,您能给我建议一些解决方案吗?我不知道其他加载fxml文件的方法

public class MainController {

    private Patient model;
    @FXML
    private Button newPatientButton;
    @FXML
    private Button newVisitButton;
    @FXML
    private Button showVisitsButton;
    @FXML
    private Button showPatientsButton;
    @FXML
    private AnchorPane lowerAnchorPane;

    public void initData(Patient model)
    {
        this.model = model;
    }


    @FXML
    void showPatients(ActionEvent event) throws IOException { // when button pressed
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ListOfPatients.fxml"));
        lowerAnchorPane = loader.load();

    }

您可以插入
拆分窗格
,并用加载的窗格替换现有的下锚窗格:

@FXML
private SplitPane splitPane ; // with the corresponding fx:id in the fxml file

@FXML
void showPatients(ActionEvent event) throws IOException { // when button pressed
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ListOfPatients.fxml"));
    lowerAnchorPane = loader.load();

    // assuming the existing pane is the second one in the split pane:
    splitPane.getItems().set(1, lowerAnchorPane);
}

set()函数包含两个参数:索引和要替换的对象。你怎么知道最下面的那个平面是1号(索引)?@Michael213我假设它是第二个加上去的,因为你说它是(两个中)最下面的那个。