Java:当两者都在同一个包中时,如何从当前应用程序启动独立应用程序? [英] Java: How do I start a standalone application from the current one when both are in the same package?

查看:191
本文介绍了Java:当两者都在同一个包中时,如何从当前应用程序启动独立应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来应该很简单,所以我必须遗漏一些明显的东西:我在同一个包中有4个独立的应用程序,us.glenedwards.myPackage,

This seems like it should be easy, so I must be missing something obvious: I have 4 standalone applications in the same package, us.glenedwards.myPackage,


  • myClass1 extends Application

  • myClass2 extends Application

等。 。

我需要每个类都作为自己的独立应用程序。然而,我希望能够通过点击链接启动我所在的其他3个班级。 Android允许我使用Intents:

I need each class to act as its own standalone application. Yet I want to be able to start the other 3 classes from the one I'm in by clicking a link. Android allows me to do this using Intents:

Intent intent = new Intent(this, EditData.class);
overridePendingTransition(R.layout.edit_data_scrollview, R.layout.state);
startActivity(intent);

我尝试使用

myClass2.launch();

但是我收到错误,应用程序启动时一定不能不止一次被称为。我可以让它工作的唯一方法是从myClass2中删除extends application和start()方法,这意味着myClass2不再是一个独立的应用程序。

But I get an error, "Application launch must not be called more than once". The only way I can get it to work is if I remove both "extends application" and the start() method from myClass2, which means that myClass2 is no longer a standalone application.

如何从myClass1启动myClass2,myClass3或myClass4,其中所有4个都是独立的应用程序?

How can I start myClass2, myClass3, or myClass4 from myClass1 with all 4 of them being standalone applications?

推荐答案

可以通过直接在应用程序之一的新实例上调用 start(...)来实现此功能子类,但它有点像hack,并且与 start(...)方法的预期用法相反。 (只是在语义上:一个名为 start 的方法在一个名为 Application 的类中应该在你的应用程序启动时执行,而不是在某些在它已经运行之后的任意点。)

You can make this work by calling start(...) directly on a new instance of one of the Application subclasses, but it kind of feels like a bit of a hack, and is contrary to the intended use of the start(...) method. (Just semantically: a method called start in a class called Application should be executed when your application starts, not at some arbitrary point after it is already running.)

你应该真的想到 start 方法作为替代传统Java应用程序中的 main 方法。如果你有一个应用程序调用另一个应用程序的方法,你会(希望)得出你的结构不正确的结论。

You should really think of the start method as the replacement for the main method in a traditional Java application. If you had one application calling another application's main method, you would (hopefully) come to the conclusion that you had structured things incorrectly.

所以我建议重构你的设计,这样你的各个组件就不是应用程序的子类,而只是普通的常规类:

So I would recommend refactoring your design so that your individual components are not application subclasses, but just plain old regular classes:

public class FirstModule {

    // can be any Parent subclass:
    private BorderPane view ;

    public FirstModule() {

        // create view; you could also just load some FXML if you use FXML
        view = new BorderPane();

        // configure view, populate with controls, etc...

    }

    public Parent getView() {
        return view ;
    }

    // other methods as needed...
}

,同样地,

public class SecondModule {

    private GridPane view ;

    public SecondModule {

        view = new GridPane();
        // etc etc
    }

    public Parent getView() {
        return view ;
    }
}

现在你可以做一些事情,比如

Now you can just do things like

FirstModule firstModule = new FirstModule();
Scene scene = new Scene(firstModule.getView());
Stage stage = new Stage();
stage.setScene(scene);
stage.show();

您需要做的任何地方。因此,您可以为每个模块创建独立应用程序:

anywhere you need to do them. So you can create standalone applications for each module:

public class FirstApplication extends Application {

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(new FirstModule().getView());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

或者您可以将它们实例化为更大的应用程序的一部分:

or you can instantiate them as part of a bigger application:

public class CompositeModule {

    private HBox view ;

    public CompositeModule() {

        Button first = new Button("First Module");
        first.setOnAction(e -> {
            Parent view = new FirstModule().getView();
            Scene scene = new Scene(view);
            Stage stage = new Stage();
            stage.initOwner(first.getScene().getWindow());
            stage.setScene(scene);
            stage.show();
        });

        Button second = new Button("Second Module");
        second.setOnAction(e -> {
            Parent view = new SecondModule().getView();
            Scene scene = new Scene(view);
            Stage stage = new Stage();
            stage.initOwner(second.getScene().getWindow());
            stage.setScene(scene);
            stage.show();
        });

        HBox view = new HBox(10, first, second);
        view.setAlignment(Pos.CENTER);

    }

    public Parent getView() {
        return view ;
    }
}

public class CompositeApplication extends Application {
    @Override
    public void start(Stage primaryStage) {

        Scene scene = new Scene(new CompositeModule().getView(), 360, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我想到的方式是 Application 子类代表整个正在运行的应用程序。因此,只有每个JVM一次实例化一个这样的类才有意义,所以你应该认为这些本身不是可重用的。将您想要重用的任何代码移动到某个不同的类中。

The way I think of this is that Application subclasses represent an entire running application. Consequently it makes sense only to ever instantiate one such class once per JVM, so you should consider these inherently not to be reusable. Move any code you want to reuse into a different class somewhere.

这篇关于Java:当两者都在同一个包中时,如何从当前应用程序启动独立应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆