使用JavaFX8显示FXML时执行操作

使用JavaFX8显示FXML时执行操作,java,javafx-8,fxml,Java,Javafx 8,Fxml,我正在构建一个JavaFX应用程序,它具有多个屏幕,因此具有多个FXML文件及其控制器。当应用程序启动所有FXML文件时,控制器的初始化方法已完成 我有一个锁屏,用户需要输入密码才能进入下一个屏幕。密码正确后,将检索员工姓名并加载主菜单屏幕 我想将用户的名称传递给主菜单屏幕中的标签,但是我不能使用控制器的初始化方法,因为它已经被调用了 有没有一种方法可以在显示FXML屏幕时执行一个操作,使我能够在控制器之间传递字符串 非常感谢您的帮助 如果你想看到代码,请在下面评论 编辑(为了更好地理解) 您可

我正在构建一个JavaFX应用程序,它具有多个屏幕,因此具有多个FXML文件及其控制器。当应用程序启动所有FXML文件时,控制器的初始化方法已完成

我有一个锁屏,用户需要输入密码才能进入下一个屏幕。密码正确后,将检索员工姓名并加载主菜单屏幕

我想将用户的名称传递给主菜单屏幕中的标签,但是我不能使用控制器的初始化方法,因为它已经被调用了

有没有一种方法可以在显示FXML屏幕时执行一个操作,使我能够在控制器之间传递字符串

非常感谢您的帮助

如果你想看到代码,请在下面评论

编辑(为了更好地理解)

您可以在下面找到代码:

Main.java

public class Main extends Application {

public static String screen1ID = "loginscherm";
public static String screen1File = "Lockscreen.fxml";
public static String screen2ID = "mainmenu";
public static String screen2File = "MainMenu.fxml";
//public static String screen3ID = "screen3";
//public static String screen3File = "Screen3.fxml";
public static Functions databaseConnection;

@Override
public void start(Stage stage) throws Exception {

    databaseConnection = new Functions();
    databaseConnection.DB_Connect();

    octocash.GUI_Screens.ScreensController mainContainer = new octocash.GUI_Screens.ScreensController();
    mainContainer.loadScreen(Main.screen1ID, Main.screen1File);
    mainContainer.loadScreen(Main.screen2ID, Main.screen2File);

    mainContainer.setScreen(Main.screen1ID);

    Group root = new Group(); //als je meerdere vensters in moet laden
    root.getChildren().addAll(mainContainer);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setFullScreen(true); //full screen without borders (no program menu bars)
    stage.setFullScreenExitHint(""); //Don't show "Press ESC to exit full screen"
    //stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH); //NIET AANZETTEN VOOR JE IETS ANDERS GEMAAKT HEBT ZODAT JE ERUIT KUNT
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}   
}
public class LockscreenController implements Initializable, ControlledScreen {

ScreensController myController;

@FXML
private PasswordField passwordField;
public FlowPane mainContent;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

@Override
public void initialize(URL url, ResourceBundle rb) {
    mainContent.setPrefSize(screenSize.getWidth(), screenSize.getHeight());
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void goToMainMenu(){
   myController.setScreen(octocash.Main.screen2ID); 
}

public static String employeeName;
public static String employeeIsAdmin;

@FXML
private void checkPassword(ActionEvent event){
   String input = passwordField.getText();
   String needsToBeChecked;
   needsToBeChecked = (new octocash.Functions()).hashPassword(input);
   String[][] employeeInfo = octocash.Main.databaseConnection.getData("some_query", Arrays.asList("naam","isAdmin"));

   if(employeeInfo[0][0] != null) {
       employeeName = employeeInfo[0][0];
       employeeIsAdmin = employeeInfo[0][1];
       goToMainMenu();
       passwordField.setText("");
   }
   else{
       passwordField.setText("");
   } 
}   
}
public class MainMenuController implements Initializable, ControlledScreen {

ScreensController myController;

public FlowPane mainContent;
public ToolBar toolBar;
public Region spacerToolbar;
public HBox buttonHolder;
public Button exitButton;
public Label currentlyLoggedIn;
public Button testButton;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

@Override
public void initialize(URL url, ResourceBundle rb) {
    mainContent.setPrefSize(screenSize.getWidth(), screenSize.getHeight());
    toolBar.setMinWidth(screenSize.getWidth());
    buttonHolder.setHgrow(spacerToolbar, Priority.ALWAYS); //Align buttonHolder to the right
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void exitApplication(){
    Stage stage = (Stage) exitButton.getScene().getWindow();
    stage.close();
} 
}
LockscreenController.java

public class Main extends Application {

public static String screen1ID = "loginscherm";
public static String screen1File = "Lockscreen.fxml";
public static String screen2ID = "mainmenu";
public static String screen2File = "MainMenu.fxml";
//public static String screen3ID = "screen3";
//public static String screen3File = "Screen3.fxml";
public static Functions databaseConnection;

@Override
public void start(Stage stage) throws Exception {

    databaseConnection = new Functions();
    databaseConnection.DB_Connect();

    octocash.GUI_Screens.ScreensController mainContainer = new octocash.GUI_Screens.ScreensController();
    mainContainer.loadScreen(Main.screen1ID, Main.screen1File);
    mainContainer.loadScreen(Main.screen2ID, Main.screen2File);

    mainContainer.setScreen(Main.screen1ID);

    Group root = new Group(); //als je meerdere vensters in moet laden
    root.getChildren().addAll(mainContainer);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setFullScreen(true); //full screen without borders (no program menu bars)
    stage.setFullScreenExitHint(""); //Don't show "Press ESC to exit full screen"
    //stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH); //NIET AANZETTEN VOOR JE IETS ANDERS GEMAAKT HEBT ZODAT JE ERUIT KUNT
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}   
}
public class LockscreenController implements Initializable, ControlledScreen {

ScreensController myController;

@FXML
private PasswordField passwordField;
public FlowPane mainContent;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

@Override
public void initialize(URL url, ResourceBundle rb) {
    mainContent.setPrefSize(screenSize.getWidth(), screenSize.getHeight());
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void goToMainMenu(){
   myController.setScreen(octocash.Main.screen2ID); 
}

public static String employeeName;
public static String employeeIsAdmin;

@FXML
private void checkPassword(ActionEvent event){
   String input = passwordField.getText();
   String needsToBeChecked;
   needsToBeChecked = (new octocash.Functions()).hashPassword(input);
   String[][] employeeInfo = octocash.Main.databaseConnection.getData("some_query", Arrays.asList("naam","isAdmin"));

   if(employeeInfo[0][0] != null) {
       employeeName = employeeInfo[0][0];
       employeeIsAdmin = employeeInfo[0][1];
       goToMainMenu();
       passwordField.setText("");
   }
   else{
       passwordField.setText("");
   } 
}   
}
public class MainMenuController implements Initializable, ControlledScreen {

ScreensController myController;

public FlowPane mainContent;
public ToolBar toolBar;
public Region spacerToolbar;
public HBox buttonHolder;
public Button exitButton;
public Label currentlyLoggedIn;
public Button testButton;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

@Override
public void initialize(URL url, ResourceBundle rb) {
    mainContent.setPrefSize(screenSize.getWidth(), screenSize.getHeight());
    toolBar.setMinWidth(screenSize.getWidth());
    buttonHolder.setHgrow(spacerToolbar, Priority.ALWAYS); //Align buttonHolder to the right
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void exitApplication(){
    Stage stage = (Stage) exitButton.getScene().getWindow();
    stage.close();
} 
}
MainMenuController.java

public class Main extends Application {

public static String screen1ID = "loginscherm";
public static String screen1File = "Lockscreen.fxml";
public static String screen2ID = "mainmenu";
public static String screen2File = "MainMenu.fxml";
//public static String screen3ID = "screen3";
//public static String screen3File = "Screen3.fxml";
public static Functions databaseConnection;

@Override
public void start(Stage stage) throws Exception {

    databaseConnection = new Functions();
    databaseConnection.DB_Connect();

    octocash.GUI_Screens.ScreensController mainContainer = new octocash.GUI_Screens.ScreensController();
    mainContainer.loadScreen(Main.screen1ID, Main.screen1File);
    mainContainer.loadScreen(Main.screen2ID, Main.screen2File);

    mainContainer.setScreen(Main.screen1ID);

    Group root = new Group(); //als je meerdere vensters in moet laden
    root.getChildren().addAll(mainContainer);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setFullScreen(true); //full screen without borders (no program menu bars)
    stage.setFullScreenExitHint(""); //Don't show "Press ESC to exit full screen"
    //stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH); //NIET AANZETTEN VOOR JE IETS ANDERS GEMAAKT HEBT ZODAT JE ERUIT KUNT
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}   
}
public class LockscreenController implements Initializable, ControlledScreen {

ScreensController myController;

@FXML
private PasswordField passwordField;
public FlowPane mainContent;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

@Override
public void initialize(URL url, ResourceBundle rb) {
    mainContent.setPrefSize(screenSize.getWidth(), screenSize.getHeight());
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void goToMainMenu(){
   myController.setScreen(octocash.Main.screen2ID); 
}

public static String employeeName;
public static String employeeIsAdmin;

@FXML
private void checkPassword(ActionEvent event){
   String input = passwordField.getText();
   String needsToBeChecked;
   needsToBeChecked = (new octocash.Functions()).hashPassword(input);
   String[][] employeeInfo = octocash.Main.databaseConnection.getData("some_query", Arrays.asList("naam","isAdmin"));

   if(employeeInfo[0][0] != null) {
       employeeName = employeeInfo[0][0];
       employeeIsAdmin = employeeInfo[0][1];
       goToMainMenu();
       passwordField.setText("");
   }
   else{
       passwordField.setText("");
   } 
}   
}
public class MainMenuController implements Initializable, ControlledScreen {

ScreensController myController;

public FlowPane mainContent;
public ToolBar toolBar;
public Region spacerToolbar;
public HBox buttonHolder;
public Button exitButton;
public Label currentlyLoggedIn;
public Button testButton;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

@Override
public void initialize(URL url, ResourceBundle rb) {
    mainContent.setPrefSize(screenSize.getWidth(), screenSize.getHeight());
    toolBar.setMinWidth(screenSize.getWidth());
    buttonHolder.setHgrow(spacerToolbar, Priority.ALWAYS); //Align buttonHolder to the right
}

public void setScreenParent(ScreensController screenParent){
    myController = screenParent;
}

@FXML
private void exitApplication(){
    Stage stage = (Stage) exitButton.getScene().getWindow();
    stage.close();
} 
}

现在:我想从LockscreenController中获取employeeName的值,并将其发送到MainMenuController,而不使用initialize方法。

您必须找到一种方法来链接它们之间的控制器,或者只链接必须访问其他控制器的控制器,并且必须实现如下方法

mainController.refreshMainMenuLabel(用户)

因此,当用户登录时,该控制器将调用mainController的refreshMainMenuLabel

编辑

我给你举一个有两个控制器的例子

首先定义AppContext

 public static class AppContext{

            //you can add the controllers by their variables 
            private Controller1 test1;
            private Controller2 test2;

            //or from a list which will require handling , but it will be more dynamic
              private List<Controller> controllers;
            private static AppContext context=null;
            //You make the constructor private so its really a sigleton ,
            //cause noone can access it from outer package
            private AppContext()
            {

            }

           public static AppContext getAppContext(){
               if(context==null)
                      context=new AppContext();
               return context;
           }

           public void setController1(Controller1 e)
           {
               test1=e;
           }

           public void setController2(Controller2 e)
           {
               test2=e;
           }

           public Controller1 getController1()
           {
               return test1;
           }

           public Controller2 getController2()
           {
               return test2;
           }  

        }
公共静态类AppContext{
//可以通过其变量添加控制器
私人控制器1测试1;
私人控制员2测试2;
//或者从一个需要处理的列表中,但它将更加动态
私有列表控制器;
私有静态AppContext上下文=null;
//你把构造函数设为私有的,所以它实际上是一个sigleton,
//因为没有人能从外包装上取到它
私有AppContext()
{
}
公共静态AppContext getAppContext(){
if(上下文==null)
context=新的AppContext();
返回上下文;
}
公共无效设置控制器1(控制器1 e)
{
test1=e;
}
公共无效设置控制器2(控制器2 e)
{
test2=e;
}
公共控制器1 getController1()
{
返回test1;
}
公共控制器2 getController2()
{
返回test2;
}  
}
因此,这些方法可以像AppContext.getAppContext().getController1()一样从应用程序中的任何地方调用,因为getAppContext是静态的,
如果您有任何问题,请告诉我

也许
mainMenuScreenController.setUserLabel(loggedInUserName)
你知道控制器之间如何传输数据吗?@UlukBiy不仅用户名需要它,以后其他字符串变量也需要它。@ItachiUchiha不,这正是我想知道的。@bashoogzaad请过目,如果你还有疑问,请联系usThanks征求意见,但我不知道如何连接控制器。我已经知道我可能需要实现的方法。最简单但不是最好的方法是将它们传递到控制器的构造函数中,或者您可以创建一个单例类,从中设置并获取活动控制器,该类将可用于您项目中的所有其他类,因此您可以在运行时引用所有控制器。您能否举例说明如何创建这样一个单例类,以及在何处定义它?谢谢您的示例!我现在可以从第一个控制器在AppContext中设置变量的值,但是我无法在第二个控制器中获得它,因为我无法将它放入initialize方法中,因为它已经被执行了。initialize方法已经执行,因为所有屏幕都是在应用程序启动时加载的,并放在StackPane中。我使用了这样的方法:init方法用于初始化帧,当用户登录时,您将不会再次初始化帧,因为它已经初始化,但您将从其控制器调用loginUser()方法,该方法将刷新已经初始化的帧的内容