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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM