JavaFX 中具有 1 个阶段和多个场景的登录应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23627340/
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-08-14 00:23:07  来源:igfitidea点击:

Login Application with 1 stage and multiple scene in JavaFX

javajavafxfxmlstagescenebuilder

提问by jabbeboy

I am doing a timeline project. I have successfully created a login system and menus for everything, but when I press the buttons I have done so it will open a new window(with stage, scenes). I have read that it isn't the best approach. The best way would be to only have 1 primary stage, and that one would be when I launch the application, the login.

我正在做一个时间线项目。我已经成功地为所有内容创建了登录系统和菜单,但是当我按下按钮时,它会打开一个新窗口(带有舞台、场景)。我读过这不是最好的方法。最好的方法是只有 1 个主要阶段,当我启动应用程序时,即登录。

But I have looked for information about multiple scenes with one stage but I have not found any good solutions. Would really really appreciate some help ;) Hopefully you understand what I want to achieve. Worth mentioning, i=I'm dealing with Scenebuilder and fxml files so I all I want to basically do is load a new .fxml scene onto the primary stage.

但是我找了一个阶段的多个场景的信息,但没有找到任何好的解决方案。真的很感激一些帮助;) 希望你明白我想要实现的目标。值得一提的是,我=我正在处理 Scenebuilder 和 fxml 文件,所以我想要做的基本上就是将一个新的 .fxml 场景加载到初级阶段。

So I have looked in another thread and try to do a VistaFramework that handles all of the scene changes. But I don't understand it fully, and I cant get it to work.

所以我查看了另一个线程并尝试做一个处理所有场景变化的 VistaFramework。但我不完全理解它,我无法让它发挥作用。

package application;

import javafx.fxml.FXMLLoader;

import java.io.IOException;

import controllers.MainController;

/**
 * Utility class for controlling navigation between vistas.
 *
 * All methods on the navigator are static to facilitate
 * simple access from anywhere in the application.
 */
public class VistaNavigator {

    /**
     * Convenience constants for fxml layouts managed by the navigator.
     */
    public static final String MAIN    = "LoginGUI.fxml";
    public static final String NEW_USER = "NewUserGUI.fxml";
    public static final String STARTMENU = "StartMenuGUI.fxml";

    /** The main application layout controller. */
    private static MainController mainController;

    /**
     * Stores the main controller for later use in navigation tasks.
     *
     * @param mainController the main application layout controller.
     */
    public static void setMainController(MainController mainController) {
        VistaNavigator.mainController = mainController;
    }

    /**
     * Loads the vista specified by the fxml file into the
     * vistaHolder pane of the main application layout.
     *
     * Previously loaded vista for the same fxml file are not cached.
     * The fxml is loaded anew and a new vista node hierarchy generated
     * every time this method is invoked.
     * @param fxml the fxml file to be loaded.
     */

    public static void loadVista(String fxml) {
        try {
            mainController.setVista(
                FXMLLoader.load(
                    VistaNavigator.class.getResource(
                        fxml
                    )
                )
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

I get a error in loadVista(). Get the following error at mainController.setVista( "The method setVista(Node) in the type MainController is not applicable for the arguments (Object)"

我在 loadVista() 中遇到错误。在 mainController.setVista(“MainController 类型中的方法 setVista(Node) 不适用于参数 (Object)”时出现以下错误)

回答by ItachiUchiha

Each FXML file is not necessarily a new Scene.

每个 FXML 文件不一定是一个新场景。

A fxml is just a view file with its root elementas any of the Layoutsprovided by Javafx. It may have multiple Layouts(as a part of the root layout) and controls depending on your requirement.

fxml 只是一个视图文件,它root element与Javafx 提供的任何布局一样。根据您的要求,它可能有多个布局(作为根布局的一部分)和控件。

To know more about fxml, you can view

想了解更多关于fxml,可以查看

Java vs JavaFX Script vs FXML. Which is better way of programming in JavaFX?

Java vs JavaFX Script vs FXML。在 JavaFX 中哪种编程方式更好?

Tutorial on FXML

FXML 教程

http://docs.oracle.com/javafx/2/fxml_get_started/jfxpub-fxml_get_started.htm

http://docs.oracle.com/javafx/2/fxml_get_started/jfxpub-fxml_get_started.htm

Now, once your FXML is ready, you can load it in different ways :

现在,一旦您的 FXML 准备就绪,您就可以以不同的方式加载它:

  1. Load as a root of your scene
  2. Load as a part of the already loaded LAYOUT
  3. Load as the root of the new Scene and assign it to the current stage
  1. 作为场景的根加载
  2. 作为已加载 LAYOUT 的一部分加载
  3. 作为新场景的根加载并将其分配给当前阶段

To help you understand the above points here is an example for each of them. Here, I am demonstrating a LoginControllerclass which is a Controller for loading the FXML.

为帮助您理解上述要点,这里为每个要点提供了一个示例。在这里,我正在演示一个LoginController类,它是一个用于加载FXML.

Example - 1

示例 - 1

FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
Scene scene = new Scene(login); 

Example - 2

示例 - 2

FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
BorderPane borderPane = (BorderPane)scene.getRoot();
borderPane.setCenter(login);

Example - 3

示例 - 3

FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
Scene scene = new Scene(login);
stage.setScene(scene);//Stage loads the new scene, which has the layout of the fxml

N.B.For more details on how to access Stage/Sceneon different controllers please go through

注意有关如何访问Stage/Scene不同控制器的更多详细信息,请访问

https://community.oracle.com/message/11251866

https://community.oracle.com/message/11251866