JavaFX切换场景

时间:2017-01-31 13:26:05

标签: java javafx scene

我一直试图让我的应用程序在场景之间切换。这是部分代码的副本。信用场景只有一个后退按钮,应该返回到主场景。

当我尝试点击主场景上的点数按钮时,它变成白色的白色屏幕。我相信有一个更好的方法来解决这个问题,你能给我一些建议吗?

public class Application {
public static void main(String[] args) {
    javafx.application.Application.launch(GUI.class);
}
}


public class GUI extends Application {

@Override
public void start(Stage primaryStage) {
    Scene mainScene, creditsScene = null;
    mainScene = getMainScene(primaryStage, creditsScene);
    creditsScene = getCreditsScene(primaryStage, mainScene);
    primaryStage.setTitle("Test application");
    primaryStage.setScene(mainScene);
    primaryStage.show();
}

private Scene getMainScene(Stage primaryStage, Scene creditsScene) {
 final Button credits = new Button("Credits");
    credits.setOnAction((ActionEvent e) -> {
        primaryStage.close();
        primaryStage.setScene(creditsScene);
        primaryStage.show();
    });
    VBox x = new VBox(50);
    x.setAlignment(Pos.CENTER);

    x.getChildren().addAll( run, displayInfo,
            label1, displayInfo, textField, submitName, credits, exit);

    //scene size
    Scene scene = new Scene(x, 650, 900);

    return scene;
}


 private Scene getCreditsScene(Stage primaryStage, Scene main) {
    final Button back = new Button("Back");
    back.setOnAction((ActionEvent e) -> {
        primaryStage.setScene(main);
    });
    VBox x = new VBox(50);
    x.getChildren().addAll(back);
    Scene credits = new Scene(x, 650, 900);
    return credits;
}

1 个答案:

答案 0 :(得分:1)

尝试切换字符串的顺序:

mainScene = getMainScene(primaryStage, creditsScene);
creditsScene = getCreditsScene(primaryStage, mainScene);

这里传递给getMainScene null