首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX中一个阶段多场景登录应用程序

JavaFX中一个阶段多场景登录应用程序
EN

Stack Overflow用户
提问于 2014-05-13 09:22:27
回答 1查看 35.7K关注 0票数 9

我在做一个时间表项目。我已经成功地为所有东西创建了一个登录系统和菜单,但是当我按下按钮时,它将打开一个新窗口(带有舞台、场景)。我读到过,这不是最好的方法。最好的方法是只有一个初级阶段,其中一个将是当我启动应用程序,登录。

但我已经寻找了一个阶段的多个场景的信息,但我没有找到任何好的解决方案。希望你能理解我想要实现的目标。值得一提的是,我正在处理Scenebuilder和fxml文件,所以我想要做的就是将一个新的.fxml场景加载到初级阶段。

因此,我查看了另一个线程,并尝试执行一个处理所有场景更改的VistaFramework。但我不完全理解它,我无法使它工作。

代码语言:javascript
复制
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();
        }
    }

}

我在loadVista()中得到一个错误。在mainController.setVista处获取以下错误(“类型MainController中的方法setVista(Node)不适用于参数(对象)”

EN

回答 1

Stack Overflow用户

发布于 2014-05-13 11:23:07

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

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

要了解有关fxml的更多信息,可以查看

Java脚本和FXML。在JavaFX中哪种编程方式更好?

关于FXML的教程

started.htm

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

  1. 加载作为您的场景的根
  2. 作为已加载布局的一部分加载
  3. 加载作为新场景的根并将其分配到当前阶段。

为了帮助您理解以上各点,这里是每个要点的一个例子。这里,我演示了一个LoginController类,它是一个用于加载FXML的控制器。

示例- 1

代码语言:javascript
复制
FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
Scene scene = new Scene(login); 

示例- 2

代码语言:javascript
复制
FXMLLoader loader = new FXMLLoader(LoginController.class.getResource("root.fxml"));
AnchorPane login = (AnchorPane) loader.load();
BorderPane borderPane = (BorderPane)scene.getRoot();
borderPane.setCenter(login);

示例- 3

代码语言:javascript
复制
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.有关如何在不同控制器上访问Stage/Scene的更多详细信息,请参阅

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

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23627340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档