2017-01-31 53 views
1

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

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

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

尝试切换字符串的顺序:

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

这里传递给getMainScene null

+0

我明白,但我怎么能改变它正常工作,因为我是passng credit场景getMainScene()和主要场景getCreditsScene。这样做,他们不能被初始化 –

+5

@JasonPer使他们的领域,而不是本地变量。那么你根本不需要将它们作为参数传递。 –

+0

@James_D。或者为其中一个场景制作一个参数,这会返回给您另一个场景的参考。 –