拆分窗格中的JavaFX切换场景?

拆分窗格中的JavaFX切换场景?,java,javafx,javafx-8,Java,Javafx,Javafx 8,在底部,我有一个锚烷,然后是一个分离窗格。在左窗格中有一个listView,根据所选的列表元素,右窗格显示相应的内容。我这样做的方法是重叠锚链烷,最初将它们设置为.setVisible(false),当它们被选中时,我将它们设置为.setVisible(true),如下所示: public void listSelection() { String selection = listView.getSelectionModel().getSelectedItem();

在底部,我有一个锚烷,然后是一个分离窗格。在左窗格中有一个listView,根据所选的列表元素,右窗格显示相应的内容。我这样做的方法是重叠锚链烷,最初将它们设置为.setVisible(false),当它们被选中时,我将它们设置为.setVisible(true),如下所示:

public void listSelection() {       
    String selection = listView.getSelectionModel().getSelectedItem();
    switch(selection) {
    case "Speed of sound":
        disableOld(); // disables old AnchorePane
        response.setText("Speed of sound conversion");
        AnchorPane1.setVisible(true);   
        break;
    case "Temperature conversion":
        disableOld(); 
        response.setText("Temperature conversion");
        AnchorPane2.setVisible(true);       
        break;
    }
}
我想知道如何在视觉上产生相同的效果,但场景不同,因为我希望每个新的AnchorPane都有自己的FXML和ControllerClass

您可以实现如下内容:

你的主课:

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;
    }
    public static void main(String[] args) {launch(args); }
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";}
@FXML
private StackPane stackPaneHolder;
... 

public void setPane(Node node) {
    if (stackPaneHolder.getChildren().isEmpty()) {
        //if stackPaneHolder is empty
        stackPaneHolder.getChildren().add(node);

    } else {
        if (stackPaneHolder.getClip() != node) {
          //if stackPaneHolder is not empty then remove existing layer and add new layer
            stackPaneHolder.getChildren().remove(0);
            stackPaneHolder.getChildren().add(0, node);
        }
    }
}
@FXML
private void btnAction(ActionEvent event) throws IOException {
Navigator.load(new Navigator().getP1());
..
然后您可以创建一个单独的类调用
Navigator
来存储所有fxml路径:

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;
    }
    public static void main(String[] args) {launch(args); }
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";}
@FXML
private StackPane stackPaneHolder;
... 

public void setPane(Node node) {
    if (stackPaneHolder.getChildren().isEmpty()) {
        //if stackPaneHolder is empty
        stackPaneHolder.getChildren().add(node);

    } else {
        if (stackPaneHolder.getClip() != node) {
          //if stackPaneHolder is not empty then remove existing layer and add new layer
            stackPaneHolder.getChildren().remove(0);
            stackPaneHolder.getChildren().add(0, node);
        }
    }
}
@FXML
private void btnAction(ActionEvent event) throws IOException {
Navigator.load(new Navigator().getP1());
..
在主
FxmlController
(它是应用程序永久层的控制器,其余堆栈窗格{p1和p2}将加载到永久层上)

这是在主
FxmlController
上加载层的方式:

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;
    }
    public static void main(String[] args) {launch(args); }
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";}
@FXML
private StackPane stackPaneHolder;
... 

public void setPane(Node node) {
    if (stackPaneHolder.getChildren().isEmpty()) {
        //if stackPaneHolder is empty
        stackPaneHolder.getChildren().add(node);

    } else {
        if (stackPaneHolder.getClip() != node) {
          //if stackPaneHolder is not empty then remove existing layer and add new layer
            stackPaneHolder.getChildren().remove(0);
            stackPaneHolder.getChildren().add(0, node);
        }
    }
}
@FXML
private void btnAction(ActionEvent event) throws IOException {
Navigator.load(new Navigator().getP1());
..
然后您可以按如下按钮加载窗格:

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;
    }
    public static void main(String[] args) {launch(args); }
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";}
@FXML
private StackPane stackPaneHolder;
... 

public void setPane(Node node) {
    if (stackPaneHolder.getChildren().isEmpty()) {
        //if stackPaneHolder is empty
        stackPaneHolder.getChildren().add(node);

    } else {
        if (stackPaneHolder.getClip() != node) {
          //if stackPaneHolder is not empty then remove existing layer and add new layer
            stackPaneHolder.getChildren().remove(0);
            stackPaneHolder.getChildren().add(0, node);
        }
    }
}
@FXML
private void btnAction(ActionEvent event) throws IOException {
Navigator.load(new Navigator().getP1());
..
这就是它的工作原理:

那是。。。。非常奇怪的解决方案。。。只是说说而已。标准方法是:将选择监听器附加到列表中,删除所有内容,并将新内容重新添加到该监听器的右侧窗格中,您甚至可以加载FXML文件,如果您将实现
窗格的类放入FXML根目录中,您甚至可以使用带有有效虚拟数据的SceneBuilder。。。不要像对待网页一样对待JavaFX——这会让你产生一个可怕的、不可维护的代码混乱;而是像对待其他Java应用程序一样对待它。但这是否可能只在一个SplitPane视图中实现,而不是替换整个窗口?因为我想要一个替代方案,而不是仅仅改变主播的可见性。