Login Application with 1 stage and multiple scene in JavaFX

前端 未结 1 1217
温柔的废话
温柔的废话 2020-12-15 11:33

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(wit

相关标签:
1条回答
  • 2020-12-15 11:56

    Each FXML file is not necessarily a new Scene.

    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.

    To know more about fxml, you can view

    Java vs JavaFX Script vs FXML. Which is better way of programming in JavaFX?

    Tutorial on FXML

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

    Now, once your FXML is ready, you can load it in different ways :

    1. Load as a root of your scene
    2. Load as a part of the already loaded LAYOUT
    3. Load as the root of the new Scene and assign it to the current stage

    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.

    Example - 1

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

    Example - 2

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

    Example - 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
    

    N.B. For more details on how to access Stage/Scene on different controllers please go through

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

    0 讨论(0)
提交回复
热议问题