在没有scenebuilder的情况下在javaFX中切换场景

时间:2018-05-22 13:54:33

标签: java javafx

我在查找如何在javafX中切换场景时遇到一些麻烦。

在下面的示例中,我想在点击Button1时从Testscherm切换到Testscherm2,但我不知道如何做到这一点,我已经坚持了很久......

这是我的代码:

  

Testscherm课程:

public class Testscherm extends VBox {

    private Label label1;
    private Button Button2;
    private TextField txf1;

    public Testscherm() {
        buildGui();
    }

    public void buildGui() {
        label1 = new Label("label1");
        Button2 = new Button("click me");
        txf1 = new TextField();

        this.getChildren().addAll(label1, Button2, txf1);

        Button2.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {

            }
        });

    }
}
  

Testscherm2课程:

public class Testscherm2 extends VBox{
    private Label scherm1;
    private Button scherm2;
    private Label trol;

    public Testscherm2() {
        buildGui();
    }

    public void buildGui() {
        scherm1 = new Label("scherm1");
        scherm2 = new Button("scherm2");
        trol = new Label("trol");

        this.getChildren().addAll(scherm1, scherm2, trol);
    }
}
  

StartUp类:

public class StartUp extends Application {



 public Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {
        //eerste scherm
        Testscherm ts = new Testscherm();
        Scene scene1 = new Scene(ts, 400, 400);

        //tweede scherm
        Testscherm2 ts2 = new Testscherm2();
        Scene scene2 = new Scene(ts2, 400, 400);

        primaryStage.setTitle("TestScherm");
        primaryStage.show();
    }

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

1 个答案:

答案 0 :(得分:0)

只需在需要时创建Testscherm2实例:

public class Testscherm extends VBox {

    private Label label1;
    private Button Button2;
    private TextField txf1;

    public Testscherm() {
        buildGui();
    }

    public void buildGui() {
        label1 = new Label("label1");
        Button2 = new Button("click me");
        txf1 = new TextField();

        this.getChildren().addAll(label1, Button2, txf1);

        Button2.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                Testscherm2 ts2 = new Testscherm2();
                getScene().setRoot(ts2);
            }
        });

    }

}

public class StartUp extends Application {

    public Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {
        //eerste scherm
        Testscherm ts = new Testscherm();
        Scene scene1 = new Scene(ts, 400, 400);

        priamryStage.setScene(scene1);
        primaryStage.setTitle("TestScherm");
        primaryStage.show();
    }

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

}

如果您需要更多灵活性(例如,您希望在视图之间来回切换),请将“当前视图状态”视为视图模型:

public class CurrentViewState {


    private final Parent view1 ;
    private final Parent view2 ;

    private final ObjectProperty<Parent> currentView ;

    public CurrentViewState() {
        view1 = new Testscherm(this);
        view2 = new Testscherm2(this);
        currentView = new SimpleObjectProperty<>(view1);
    }

    public ObjectProperty<Parent> currentViewProperty() {
        return currentView ;
    }

    public final Parent getCurrentView() {
        return currentViewProperty().get();
    }

    public final void setCurrentView(Parent currentView) {
        currentViewProperty().set(currentView);
    }

    public Parent getView1() {
        return view1 ;
    }

    public Parent getView2() {
        return view2 ;
    }
}

然后:

public class Testscherm extends VBox {

    private Label label1;
    private Button Button2;
    private TextField txf1;

    private final CurrentViewState viewState ;

    public Testscherm(CurrentViewState viewState) {
        this.viewState = viewState ;
        buildGui();
    }

    public void buildGui() {
        label1 = new Label("label1");
        Button2 = new Button("click me");
        txf1 = new TextField();

        this.getChildren().addAll(label1, Button2, txf1);

        Button2.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                viewState.setCurrentView(viewState.getView2());
            }
        });

    }

}

public class StartUp extends Application {

    public Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {
        //eerste scherm

        CurrentViewState viewState = new CurrentViewState();
        Scene scene = new Scene(viewState.getCurrentView(), 400, 400);
        scene.rootProperty().bind(viewState.currentViewProperty());

        primaryStage.setScene(scene);

        primaryStage.setTitle("TestScherm");
        primaryStage.show();
    }

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

}