在JavaFX中使用1个阶段和多个场景登录应用程序 [英] Login Application with 1 stage and multiple scene in JavaFX
问题描述
我正在做一个时间轴项目。我已经成功创建了一个登录系统和菜单,但是当我按下按钮时,我会这样做,它会打开一个新窗口(带有舞台,场景)。我读过这不是最好的方法。最好的方法是只有1个主要阶段,而那个是我启动应用程序时的登录。
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.
但是我已经找到了有关一个阶段的多个场景的信息,但我没有找到任何好的解决方案。真的非常感谢一些帮助;)希望你明白我想要实现的目标。值得一提的是,i =我正在处理Scenebuilder和fxml文件,所以我想要的主要是将新的.fxml场景加载到主舞台上。
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.
所以我查看了另一个线程,尝试做一个处理所有场景变化的VistaFramework。
但是我不完全理解它,我不能让它工作。
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.
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)不适用于参数(对象)
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)"
推荐答案
每个FXML文件不一定是新场景。
fxml只是一个视图文件,其根元素
作为任何布局由Javafx提供。它可能有多个布局(作为根布局的一部分)和控制,具体取决于您的要求。
A fxml is just a view file with its root element
as any of the Layouts provided by Javafx. It may have multiple Layouts(as a part of the root layout) and controls depending on your requirement.
要了解有关fxml的更多信息,您可以查看
To know more about fxml, you can view
Java vs JavaFX Script vs FXML。哪种更好的JavaFX编程方式?
FXML教程
http://docs.oracle.com/javafx/2/fxml_get_started/jfxpub-fxml_get_started.htm
现在,一旦你的FXML准备就绪,你可以用不同的方式加载它:
Now, once your FXML is ready, you can load it in different ways :
- 加载你的场景根
- 作为已加载的LAYOUT的一部分加载
- 加载作为新场景的根并将其分配给当前阶段
为了帮助您理解上述各点,这里有一个例子。在这里,我演示了一个 LoginController
类,它是一个用于加载 FXML
的控制器。
To help you understand the above points here is an example for each of them. Here, I am demonstrating a LoginController
class which is a Controller for loading the FXML
.
示例 - 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
NB 有关如何在不同控制器上访问舞台/场景
的详细信息,请通过
N.B. For more details on how to access Stage/Scene
on different controllers please go through
https://community.oracle.com/message/11251866
这篇关于在JavaFX中使用1个阶段和多个场景登录应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!