简体   繁体   中英

How to change the current scene to another in JavaFX?

在第一个按钮上有一个主舞台和一个控制器,当他单击时,我想将当前场景更改为下一场景,该怎么做?

Main.java:

@Override
public void start(Stage primaryStage) throws Exception{
    FXMLLoader loader = new FXMLLoader();
    Parent root = (Parent) loader.load(getClass().getResource("first.fxml").openStream());
    FirstController firstController = loader.getController();
    firstController.setStage(primaryStage);

    primaryStage.setTitle("Stage");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}

first.fxml:

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<GridPane fx:controller="sample.FirstController"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <Label text="first scene" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
    <Button text="second" onAction="#second" GridPane.columnIndex="0" GridPane.rowIndex="1" />
</GridPane>

FirstController.java:

private Stage mStage;

public void setStage(Stage mStage) {
    this.mStage = mStage;
}


public void second(ActionEvent actionEvent) throws IOException {
    FXMLLoader loader = new FXMLLoader();
    Parent root = (Parent)      loader.load(getClass().getResource("second.fxml").openStream());
    SecondController secondController = loader.getController();
    secondController.setStage(mStage);

    mStage.setTitle("second scene");
    mStage.setScene(new Scene(root));
    mStage.show();
}

second.fxml

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<GridPane fx:controller="sample.SecondController"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <Label text="second scene" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
    <Button text="first" onAction="#first" GridPane.columnIndex="0" GridPane.rowIndex="1" />
</GridPane>

SecondController.java

private Stage mStage;

public void setStage(Stage mStage) {
    this.mStage = mStage;
}


public void first(ActionEvent actionEvent) throws IOException {
    FXMLLoader loader = new FXMLLoader();
    Parent root = (Parent) loader.load(getClass().getResource("first.fxml").openStream());
    FirstController firstController = loader.getController();
    firstController.setStage(mStage);

    mStage.setTitle("first scene");
    mStage.setScene(new Scene(root));
    mStage.show();
}

And second.fxml can be any fxml you like. It is realy simple, you just need to get controller from the loader and pass instance of your Application class to it.

Note that this is short verision of my code and I had to delete some stuff for the simplicity, so it might won't work if you just copy&paste. If I missed anything let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM