JavaFX how to change stage

后端 未结 4 1218
不知归路
不知归路 2021-01-31 11:51

I\'m using Netbeans 7.2 with Scene Builder 1.0 to develop a JavaFX application. I have my main screen set up, and I want to have it so I click a button and it\'ll close the main

4条回答
  •  鱼传尺愫
    2021-01-31 12:35

    You can do it this way too:

    Modify your controller.java this way

    Stage thisStage;
    
    public void setStage (Stage stage){
        thisStage = stage;
    }
    
    public void showStage(){
        thisStage.setTitle("Titel in der MainController.java geändert");
        thisStage.show();
    }
    

    It is important that you don't use thisStage in the initialize method.

    Modify your Mainclass this way:

    @Override
    public void start(Stage stage) throws Exception {
        URL location = getClass().getResource("Main.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        Parent root = (Parent) fxmlLoader.load(location.openStream());
        Scene scene = new Scene(root);
        stage.setScene(scene);
        MainController mainController = fxmlLoader.getController();
        mainController.setStage(stage);
        mainController.showStage();
    }
    

    You can download a example projekt (netbeans) here.

提交回复
热议问题