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