[英]Login Application with 1 stage and multiple scene in JavaFX
我正在做一个时间表项目。 我已经成功地为所有内容创建了一个登录系统和菜单,但是当我按下我已经完成的按钮时,它将打开一个新窗口(带有舞台,场景)。 我读过这不是最好的方法。 最好的方法是只有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准备就绪,你可以用不同的方式加载它:
为了帮助您理解上述几点,每个例子都是一个例子。 在这里,我演示了一个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
更多详细信息,请通过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.