java JavaFX:在 UI 屏幕之间导航的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40750526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:26:47  来源:igfitidea点击:

JavaFX: Best practice for navigating between UI screens

javajavafxfxmlscenestage

提问by MordechayS

I want to change UI screens from login.fxmlto home.fxml.

我想将 UI 屏幕从 更改login.fxmlhome.fxml

Should I change the Stageor the Scene? I'm not sure which is the best practice? Also, can I use a lambda expression for the handler in the controller?

我应该更改Stage还是Scene?我不确定哪个是最佳实践?另外,我可以为控制器中的处理程序使用 lambda 表达式吗?

回答by MordechayS

First, let's start out with the Stage.vs. Sceneissue:

首先,让我们从Stage.vs开始。Scene问题:

As known, the JavaFXhierarchy is based on: Stage-> Scene-> Nodes(etc).

众所周知,JavaFX层次结构基于:Stage-> Scene-> Nodes(等)。

See here:

看这里:

enter image description here

在此处输入图片说明

Practically speaking, a rule-of-thumb in my opinion is the future:

实际上,在我看来,经验法则是未来

  • If you plan on going forward to a different placein the flow of your program (login -> profile, for example) - change the Stage.

  • If you are in the same enviroment(login for the first time -> login after multiple wrong tries) - change the Scene.

  • 如果您计划前进到程序流程中的不同位置(例如,登录 -> 配置文件) - 将Stage.

  • 如果您处于相同的环境中(第一次登录 -> 多次错误尝试后登录) - 更改Scene.

As for lambdas: Ahhmmm... if your current Java/JavaFXversion has the abillity - there is no reason not to use. For more about controller handlers in Java 8, see this great tutorial.

至于 lambdas:啊哈...如果您当前的Java/JavaFX版本有能力 - 没有理由不使用。有关 Java 8 中的控制器处理程序的更多信息,请参阅这个很棒的教程

回答by PeterB

I use this approach for changing scenes in JavaFX:

我使用这种方法来改变场景JavaFX

/**
 * Controller class for menuFrame.fxml
 */
public class MenuFrameControl implements Initializable {

    @FXML private Button sceneButton1;
    @FXML private Button sceneButton2;
    @FXML private Button sceneButton3;

   /**
     * Event handling method, loads new scene from .fxml file
     * according to clicked button and initialize all components.
     * @param event
     * @throws IOException
     */
    @FXML
    private void handleMenuButtonAction (ActionEvent event) throws IOException {
        Stage stage = null;
        Parent myNewScene = null;

        if (event.getSource() == sceneButton1){
            stage = (Stage) sceneButton1.getScene().getWindow();
            myNewScene = FXMLLoader.load(getClass().getResource("/mvc/view/scene1.fxml"));
        } else if (event.getSource() == sceneButton2){
            stage = (Stage) flightBtn.getScene().getWindow();
            myNewScene = FXMLLoader.load(getClass().getResource("/mvc/view/scene2.fxml"));
        } else if (event.getSource() == sceneButton3) {
            stage=(Stage) staffBtn.getScene().getWindow();
            myNewScene = FXMLLoader.load(getClass().getResource("/mvc/view/scene3.fxml"));
        }

        Scene scene = new Scene(myNewScene);
        stage.setScene(scene);
        stage.setTitle("My New Scene");
        stage.show();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) { }

So basically when you click the button, it saves actually displayed Stageobject into stagevariable. Then it loads new Sceneobject from .fxml file into myNewScenevariable and then put this fresh loaded Sceneobject into your saved Stageobject.

所以基本上当你点击按钮时,它会将实际显示的Stage对象保存到stage变量中。然后它将Scene.fxml 文件中的新对象加载到myNewScene变量中,然后将这个新加载的Scene对象放入您保存的Stage对象中。

With this code you can make basic three button menu, where each button switch to different scene, using just single Stageobject.

使用此代码,您可以制作基本的三按钮菜单,其中每个按钮只需使用单个Stage对象即可切换到不同的场景。