2017-10-13 2 views
0

JavaFX에서 장면 전환을 시도하고 있지만 하드 코딩했을 때 작동하지 않는 것처럼 보였습니다. 람다 식을 사용하여 작업 할 수있었습니다.JavaFX (FXML) 오류 장면 전환

public class Main extends Application { 

Stage window; 
Scene scene1; 
Scene scene2; 

public static void main(String[] args) { 
    launch(args); 
} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    window = primaryStage; 

    Label label = new Label("Welcome to the first scene"); 
    Button bttn1 = new Button("Go to second scene"); 
    bttn1.setOnAction(e -> window.setScene(scene2)); 

    //Scene 1 
    VBox layout1 = new VBox(20); 
    layout1.getChildren().addAll(label, bttn1); 
    scene1 = new Scene(layout1, 400, 400); 

    //Scene 2 
    Button bttn2 = new Button("Go to first scene"); 
    bttn2.setOnAction(e -> window.setScene(scene1)); 

    StackPane layout2 = new StackPane(); 
    layout2.getChildren().add(bttn2); 
    scene2 = new Scene(layout2, 400, 500); 

    window.setScene(scene1); 
    window.setTitle("Test"); 
    window.show(); 
} 

프로젝트가 몇 가지 GUI를 포함하고 그들에게 FX 방식을 하드 코딩하는 것보다 내가 FXML 장면 Builder에서의 GUI의 디자인을 선호하지만. 그러나 FXML 방식을 시도했을 때 버튼을 눌렀을 때 오류가 항상 나타나지 않았습니다.

오류 메시지가 part 1 part 2

이 문서 컨트롤러 코드입니다.

public class FXMLDocumentController implements Initializable { 

@FXML 
private Button button1; 

@FXML 
private Button button2; 

@FXML 
private void handleButtonAction(ActionEvent event) throws IOException { 
    Stage stage; 
    Parent root; 

    if(event.getSource() == button1){ 
     stage=(Stage)button1.getScene().getWindow(); 

     root = FXMLLoader.load(getClass().getResource("FXML2.fxml")); 
    } 
    else{ 
     stage=(Stage)button2.getScene().getWindow(); 
     root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 
    } 
    Scene scene = new Scene(root); 
    stage.setScene(scene); 
    stage.show(); 

} 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
}  

} 
+0

귀하의 게시물에서,'초기화 가능'및'공용 클래스 메인 확장 응용 프로그램'은 코드 형식으로되어 있지 않습니다. – SteveFest

+0

죄송합니다. 지금 고칠 것입니다. 그 자리에 고마워. – HoiSinz

답변

0

게시 된 오류로 인해 코드가 버튼을 앵커 패널로로드하려고합니다. fx가있는 앵커 판이 있는지 확인하십시오. 나는 button1을 사용합니다.

+0

나는 당신을 감사하고 당신을 알릴 것이다. – HoiSinz