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