Javafx 切换界面的两种方式

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

 

posted @ 2020-06-07 21:51  翡翠绿  阅读(5463)  评论(1编辑  收藏  举报