在JavaFX中使用1个阶段和多个场景登录应用程序

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

但我已经找到了有关一个阶段的多个场景的信息,但我没有找到任何好的解决方案。 真的非常感谢一些帮助;)希望你明白我想要实现的目标。 值得一提的是,i =我正在处理Scenebuilder和fxml文件,所以我想要的主要是将新的.fxml场景加载到主舞台上。

所以我查看了另一个线程并尝试执行一个处理所有场景更改的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(); } } } 

我在loadVista()中收到错误。 在mainController.setVista上获取以下错误(“MainController类型中的方法setVista(Node)不适用于参数(Object)”

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

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

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

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

关于FXML的教程

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

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

  1. 加载为场景的根
  2. 作为已加载的LAYOUT的一部分加载
  3. 加载为新场景的根,并将其分配给当前舞台

为了帮助您理解上述几点,每个例子都是一个例子。 在这里,我演示了一个LoginController类,它是一个用于加载FXML的Controller。

示例 – 1

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

示例 – 2

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

示例 – 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 

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

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