AnchorPane other = FXMLLoader.load(getClass().getResource("SecondPane.fxml"));
前置条件 加载fxml通过FXMLLoader
方法一
通过setAll 方法把fxml内容加入到root节点
1 public void switch_view() throws IOException { 2 AnchorPane other = FXMLLoader.load(getClass().getResource("SecondPane.fxml")); 3 rootPane.getChildren().clear(); 4 rootPane.getChildren().setAll(other); 5 }
方法二
通过得到scene来设置root
1 public void switch_view(ActionEvent actionEvent) throws IOException { 2 AnchorPane other = FXMLLoader.load(getClass().getResource("sample.fxml")); 3 ((Button)actionEvent.getSource()).getScene().setRoot(other); 4 }
源码:https://gitee.com/dongcang/practice-demo/tree/master/SwitchScene/src/sample