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

查看:31
本文介绍了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 扩展应用程序
  • myClass2 扩展应用程序

等等...

我需要每个类都充当自己的独立应用程序.然而,我希望能够通过单击链接从我所在的课程开始其他 3 个课程.Android 允许我使用 Intent 执行此操作:

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 中删除扩展应用程序"和 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,并且这四个都是独立的应用程序?

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

推荐答案

可以通过直接在Application 子类之一,但感觉有点像黑客,并且与 start(...) 方法的预期用途相反.(仅从语义上来说:Application 类中名为 start 的方法应该在您的应用程序启动时执行,而不是在它已经运行后的某个任意时刻执行.)

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 方法的替代品.如果您让一个应用程序调用另一个应用程序的 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天全站免登陆