如何在java中的另一个类的方法中调用类的所有方法

时间:2021-06-18 04:03:31

I am designing a JavaFX application and I need to call the Application class of one of the windows in the Controller of another window.

我正在设计一个JavaFX应用程序,我需要在另一个窗口的Controller中调用其中一个窗口的Application类。

MainController.java:

public class MainController {

  @FXML
  public Button buttonLogin;

  @FXML
  public Button buttonNeuAnmelden;

  @FXML
  public void handleButtonLoginAction(ActionEvent event) {
    ((Node) (event.getSource())).getScene().getWindow().hide();
    System.out.println("LoginButton geclickt!");

  }

  @FXML
  public void handleButtonNeuAnmeldenAction(ActionEvent event) {
    ((Node) (event.getSource())).getScene().getWindow().hide();
    System.out.println("NeuAnmeldenButton Geclickt!");

  }

}

LoginApp.java:

public class LoginApp extends Application {

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) throws Exception {
    FXMLLoader loader = new FXMLLoader(
        getClass().getResource("/design/Login.fxml"));
    Parent root = loader.load();
    primaryStage.setTitle("Benutzerverwaltung");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
  }
}

I specifically need to run all of the methods of LoginApp, meaning main(String[] args) and start(Stage primaryStage) class in handleButtonLoginAction() method as if the whole class as it is has been called exactly at that point.

我特别需要在handleButtonLoginAction()方法中运行LoginApp的所有方法,这意味着main(String [] args)和start(Stage primaryStage)类,就好像在那一点上完全调用整个类一样。

How do I do this?

我该怎么做呢?

2 个解决方案

#1


2  

If I understand the question correctly, you need to refactor this quite a bit. Define a LoginView class that is independent of your Application subclass:

如果我正确理解了这个问题,你需要对此进行重构。定义一个独立于Application子类的LoginView类:

public class LoginView {

    private final Stage displayStage ;

    private final Scene scene ;

    public LoginView(Stage displayStage) throws IOException {
        this.displayStage = displayStage ;
        FXMLLoader loader = new FXMLLoader(
            getClass().getResource("/design/Login.fxml"));
        Parent root = loader.load();        
        scene = new Scene(root);
        displayStage.setScene(scene);
        displayStage.setTitle("Benutzerverwaltung");
    }

    public LoginView() throws IOException {
        this(new Stage());
    }

    public void show() {
        displayStage.show();
    }

    public void hide() {
        displayStage.hide();
    }

    // ...
}

and then your Application class looks like:

然后你的Application类看起来像:

public class LoginApp extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        LoginView loginView = new LoginView(primaryStage);
        // ...
        loginView.show();
    }
}

Your question didn't show how MainController is related to the application, but all you need to do is pass a reference to the loginView you created to the MainController, and then call loginView.show() from the method in the MainController.

您的问题没有显示MainController如何与应用程序相关,但您需要做的就是将您创建的loginView的引用传递给MainController,然后从MainController中的方法调用loginView.show()。

#2


0  

If you Don't want to refactor your software architecture you can try Reflection, and do something like:

如果您不想重构您的软件架构,可以尝试使用Reflection,并执行以下操作:

public static Method[] getAccessibleMethods(Class clazz) {
List<Method> result = new ArrayList<Method>();
while (clazz != null) {
    for (Method method : clazz.getDeclaredMethods()) {
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
            result.add(method);
        }
    }
    clazz = clazz.getSuperclass();
}
return result.toArray(new Method[result.size()]);

}

#1


2  

If I understand the question correctly, you need to refactor this quite a bit. Define a LoginView class that is independent of your Application subclass:

如果我正确理解了这个问题,你需要对此进行重构。定义一个独立于Application子类的LoginView类:

public class LoginView {

    private final Stage displayStage ;

    private final Scene scene ;

    public LoginView(Stage displayStage) throws IOException {
        this.displayStage = displayStage ;
        FXMLLoader loader = new FXMLLoader(
            getClass().getResource("/design/Login.fxml"));
        Parent root = loader.load();        
        scene = new Scene(root);
        displayStage.setScene(scene);
        displayStage.setTitle("Benutzerverwaltung");
    }

    public LoginView() throws IOException {
        this(new Stage());
    }

    public void show() {
        displayStage.show();
    }

    public void hide() {
        displayStage.hide();
    }

    // ...
}

and then your Application class looks like:

然后你的Application类看起来像:

public class LoginApp extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        LoginView loginView = new LoginView(primaryStage);
        // ...
        loginView.show();
    }
}

Your question didn't show how MainController is related to the application, but all you need to do is pass a reference to the loginView you created to the MainController, and then call loginView.show() from the method in the MainController.

您的问题没有显示MainController如何与应用程序相关,但您需要做的就是将您创建的loginView的引用传递给MainController,然后从MainController中的方法调用loginView.show()。

#2


0  

If you Don't want to refactor your software architecture you can try Reflection, and do something like:

如果您不想重构您的软件架构,可以尝试使用Reflection,并执行以下操作:

public static Method[] getAccessibleMethods(Class clazz) {
List<Method> result = new ArrayList<Method>();
while (clazz != null) {
    for (Method method : clazz.getDeclaredMethods()) {
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
            result.add(method);
        }
    }
    clazz = clazz.getSuperclass();
}
return result.toArray(new Method[result.size()]);

}