JavaFX + Scene Builder如何切换场景 [英] JavaFX + Scene Builder how switch scene

查看:1318
本文介绍了JavaFX + Scene Builder如何切换场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaFx和Scenebuilder,并希望在eclipse中为自己创建一个名为Taskplanner的本地应用程序。

I'm working with JavaFx and Scenebuilder and want create a local app for myself called "Taskplanner" in eclipse.

我创建了一个新的舞台并设置了它场景(参见Main.java)。但不确定如何在阶段设置新场景(请参阅Controller.java)。 Didnt还没有发现是否可以通过signInButtonClicked() - MethodeStage primaryStage而不是Scene Builder

I created a new Stage and set it with a Scene (see Main.java). But not sure how to set a new Scene in the old stage (see Controller.java). Didnt also not find out if it is possible pass the signInButtonClicked()-Methode the "Stage primaryStage" over Scene Builder

有人可以帮忙吗?

Controller.java:

@FXML
Button btnSignIn;

@FXML
public void signInButtonClicked() throws Exception
{
//Here I want call the new Scene(SignInGUI.fxml) in my old Stage
   FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/SignInGUI.fxml"));
}

Main.java:

        @Override
        public void start(Stage primaryStage) throws Exception 
      {
            Parent root = FXMLLoader.load(getClass().getResource("../view/LoginGUI.fxml"));

            primaryStage.setTitle("Taskplanner");
            primaryStage.setScene(new Scene(root,500,500));
            primaryStage.show();
        }


        public static void main(String[] args) {

            launch(args);
    }


推荐答案

您可以参考您的按钮引用中的场景窗口。从那里,由您来决定如何显示新视图。

You can get a reference to the Scene and Window from your button reference. From there, it's up to you to decide how to you want to show the new view.

以下是您获取这些参考的方式:

Here's how you get those references:

Scene scene = btnSignIn.getScene();
Window window = scene.getWindow();
Stage stage = (Stage) window;

您可以通过更改场景的根来更改视图:

You can change the view by changing the root of your Scene:

FXMLLoader loader = ... // create and load() view
btnSignIn.getScene().setRoot(loader.getRoot());

或者你可以改变整个场景:

Or you can change the entire Scene:

FXMLLoader loader = ... // create and load() view
Stage stage = (Stage) btnSignIn.getScene().getWindow();
Scene scene = new Scene(loader.getRoot());
stage.setScene(scene);

这篇关于JavaFX + Scene Builder如何切换场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆