1

これは簡単なように思えますが、明らかな何かが欠けているに違いありません: 同じパッケージ us.glenedwards.myPackage に 4 つのスタンドアロン アプリケーションがあります。

  • myClass1 はアプリケーションを拡張します
  • myClass2 はアプリケーションを拡張します

等...

各クラスが独自のスタンドアロン アプリケーションとして機能する必要があります。それでも、リンクをクリックして、現在のクラスから他の 3 つのクラスを開始できるようにしたいと考えています。Android では、インテントを使用してこれを行うことができます。

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

を使用して myClass1 から myClass2 を開始しようとしました

myClass2.launch("");

しかし、「アプリケーションの起動を複数回呼び出すことはできません」というエラーが表示されます。それを機能させる唯一の方法は、myClass2 から「extends application」と start() メソッドの両方を削除することです。つまり、myClass2 はスタンドアロン アプリケーションではなくなります。

myClass1 から myClass2、myClass3、または myClass4 を開始し、それらの 4 つすべてをスタンドアロン アプリケーションにする方法を教えてください。

4

3 に答える 3

4

サブクラスの 1 つの新しいインスタンスを直接呼び出すことでこれを機能させることができますが、これはちょっとしたハックのように感じられ、メソッドの意図された使用法に反します。(単に意味的に: 呼び出されたクラスで呼び出されたメソッドは、アプリケーションが既に実行されている任意の時点ではなく、アプリケーションの開始時に実行する必要があります。)start(...)Applicationstart(...)startApplication

このメソッドは、従来の Java アプリケーションstartのメソッドを置き換えるものと考える必要があります。mainあるアプリケーションが別のアプリケーションのmainメソッドを呼び出す場合、(うまくいけば) 構造化が間違っているという結論に達するでしょう。

したがって、個々のコンポーネントがアプリケーションのサブクラスではなく、単純な古い通常のクラスになるように、設計をリファクタリングすることをお勧めします。

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 ;
    }
}

これで、次のようなことができます

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

必要な場所ならどこでも。したがって、モジュールごとにスタンドアロン アプリケーションを作成できます。

public class FirstApplication extends Application {

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

または、より大きなアプリケーションの一部としてそれらをインスタンス化できます。

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 ごとに 1 回だけそのようなクラスをインスタンス化することが理にかなっているため、これらは本質的に再利用可能ではないと考える必要があります。再利用したいコードを別のクラスに移動します。

于 2015-09-08T19:41:06.280 に答える
0

これを試しましたか?

Runtime.getRuntime().exec("myClass1 [args]"); //put all args as you used in command

また、必要に応じて例外を処理/キャッチします。

于 2015-09-08T18:25:16.747 に答える
0

私が正しかった; それは簡単なことでした。それが、4時間の睡眠でコードを書くことで得られるものです:

myClass2 class2 = new myClass2(); 
try { 
 class2.start(stage); 
} catch (Exception e) { e.printStackTrace(); } }
于 2015-09-08T18:41:39.383 に答える