2017-01-31 4 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

내가 이해할 수 있지만 getMainScene() 및 getCreditsScene 메인 장면을 passng 크레딧 때문에 오전 내가 제대로 작동하도록 변경할 수 있습니다. 이렇게하면 초기화 할 수 없습니다. –

+5

@JasonPer 로컬 변수 대신 필드로 만듭니다. 그렇다면 매개 변수로 전달할 필요가 없습니다. –

+0

@James_D. 또는 다른 장면에 대한 참조를 반환하는 장면 중 하나에 매개 변수를 만드십시오. –