简体   繁体   English

如何在JavaFX中将当前场景更改为另一个场景?

[英]How to change the current scene to another in JavaFX?

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

Main.java: 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: 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: 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 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 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. 而且second.fxml可以是您喜欢的任何fxml。 It is realy simple, you just need to get controller from the loader and pass instance of your Application class to it. 这确实很简单,您只需要从加载器中获取控制器并将Application类的实例传递给它即可。

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. 如果我错过任何事情,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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