【问题标题】:JavaFX switching scenesJavaFX 切换场景
【发布时间】:2017-01-31 13:26:05
【问题描述】:

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

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

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;
}

【问题讨论】:

    标签: java javafx scene


    【解决方案1】:

    尝试切换字符串的顺序:

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

    在这里你传递给 getMainScene null

    【讨论】:

    • 我明白,但我怎么能改变它以正常工作,因为我将积分场景传递给 getMainScene() 并将主场景传递给 getCreditsScene。这样做他们不能都被初始化
    • @JasonPer 使它们成为字段而不是局部变量。那么你根本不需要将它们作为参数传递。
    • @James_D 完全正确。或者为其中一个场景创建一个参数,它会返回对另一个场景的引用。
    猜你喜欢
    • 2020-03-23
    • 2016-04-13
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2018-05-25
    • 2023-03-31
    相关资源
    最近更新 更多