我在做一个时间表项目。我已经成功地为所有东西创建了一个登录系统和菜单,但是当我按下按钮时,它将打开一个新窗口(带有舞台、场景)。我读到过,这不是最好的方法。最好的方法是只有一个初级阶段,其中一个将是当我启动应用程序,登录。
但我已经寻找了一个阶段的多个场景的信息,但我没有找到任何好的解决方案。希望你能理解我想要实现的目标。值得一提的是,我正在处理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)不适用于参数(对象)”
发布于 2014-05-13 11:23:07
每个FXML文件不一定是一个新场景。
fxml只是一个视图文件,它的root element是Javafx提供的任何一个布局。它可能有多个布局(作为根布局的一部分)和控件,这取决于您的需求。
要了解有关fxml的更多信息,可以查看
关于FXML的教程
started.htm
现在,一旦您的FXML准备就绪,您就可以以不同的方式加载它:
为了帮助您理解以上各点,这里是每个要点的一个例子。这里,我演示了一个LoginController类,它是一个用于加载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 fxmlN.B.有关如何在不同控制器上访问Stage/Scene的更多详细信息,请参阅
https://stackoverflow.com/questions/23627340
复制相似问题