How to swap screens in a JavaFX application in the controller class?

后端 未结 3 1540

If there are 3 files in a JavaFX project; an FXML file, a controller for the FXML, and an application class; how can the controller respond to a button click (which works pe

3条回答
  •  天命终不由人
    2020-11-29 07:23

    You can try like this too.

    public void onBtnClick(ActionEvent event) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("login.fxml"));
            Stage stage = (Stage) btn.getScene().getWindow();
            Scene scene = new Scene(loader.load());
            stage.setScene(scene);
        }catch (IOException io){
            io.printStackTrace();
        }
    
    }
    

提交回复
热议问题