如何在java中多次调用launch() [英] How to call launch() more than once in java

查看:59
本文介绍了如何在java中多次调用launch()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在java中多次调用launch()我得到一个异常主错误:java.lang.IllegalStateException:应用程序启动不能被多次调用"

How to call the launch() more than once in java i am given an exception as "ERROR IN MAIN:java.lang.IllegalStateException: Application launch must not be called more than once"

当请求到来时,我在我的 java 应用程序中创建了 rest cleint,它调用 javafx 并在完成 webview 操作后打开 webview 使用 Platform.exit() 方法关闭 javafx 窗口.当第二个请求出现时,我收到此错误如何解决此错误.

I have create rest cleint in my java application when request comes it call javafx and opening webview after completing webview operarion am closing javafx windows using Platform.exit() method. when second request comes am getting this error how to reslove this error.

JavaFx 应用程序代码:

JavaFx Application Code:

public class AppWebview extends Application  {

    public static Stage stage;

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

        stage = _stage;
        StackPane root = new StackPane();

        WebView view = new WebView();

        WebEngine engine = view.getEngine();
        engine.load(PaymentServerRestAPI.BROWSER_URL);
        root.getChildren().add(view);
        engine.setJavaScriptEnabled(true);
        Scene scene = new Scene(root, 800, 600);
        stage.setScene(scene);

        engine.setOnResized(new EventHandler<WebEvent<Rectangle2D>>() {
            public void handle(WebEvent<Rectangle2D> ev) {
                Rectangle2D r = ev.getData();
                stage.setWidth(r.getWidth());
                stage.setHeight(r.getHeight());
            }
        });

        JSObject window = (JSObject) engine.executeScript("window");
        window.setMember("app", new BrowserApp());

        stage.show();

    }

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

RestClient 方法:调用 JavaFX 应用程序

RestClient Method: Calling to JavaFX application

// method 1 to lanch javafx
javafx.application.Application.launch(AppWebview.class);

// method 2 to lanch javafx
String[] arguments = new String[] {"123"};
AppWebview .main(arguments);

推荐答案

不能调用launch() 在 JavaFX 应用程序上不止一次,这是不允许的.

You can't call launch() on a JavaFX application more than once, it's not allowed.

来自 javadoc:

From the javadoc:

It must not be called more than once or an exception will be thrown.

建议定期显示窗口

  1. 只需调用一次 Application.launch().
  2. 使用 Platform.setImplicitExit(false),这样当你隐藏最后一个应用程序窗口时,JavaFX 不会自动关闭.
  3. 下次需要另一个窗口时,将窗口包装起来 show() 调用 Platform.runLater(),以便在 JavaFX 应用程序线程上执行调用.莉>

有关此方法的简短摘要:

For a short summary implementation of this approach:

如果您正在混合 Swing,您可以使用 JFXPanel 而不是 应用程序,但使用模式将与上面概述的类似.

If you are mixing Swing you can use a JFXPanel instead of an Application, but the usage pattern will be similar to that outlined above.

Wumpus 示例

这个例子比它需要的要复杂一些,因为它也涉及定时器任务.但是,它确实提供了一个完整的独立示例,有时可能会有所帮助.

This example is bit more complicated than it needs to be because it also involves timer tasks. However it does provide a complete stand-alone example, which might help sometimes.

import javafx.animation.PauseTransition;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.*;

// hunt the Wumpus....
public class Wumpus extends Application {
    private static final Insets SAFETY_ZONE = new Insets(10);
    private Label cowerInFear = new Label();
    private Stage mainStage;

    @Override
    public void start(final Stage stage) {
        // wumpus rulez
        mainStage = stage;
        mainStage.setAlwaysOnTop(true);

        // the wumpus doesn't leave when the last stage is hidden.
        Platform.setImplicitExit(false);

        // the savage Wumpus will attack
        // in the background when we least expect
        // (at regular intervals ;-).
        Timer timer = new Timer();
        timer.schedule(new WumpusAttack(), 0, 5_000);

        // every time we cower in fear
        // from the last savage attack
        // the wumpus will hide two seconds later.
        cowerInFear.setPadding(SAFETY_ZONE);
        cowerInFear.textProperty().addListener((observable, oldValue, newValue) -> {
            PauseTransition pause = new PauseTransition(
                    Duration.seconds(2)
            );
            pause.setOnFinished(event -> stage.hide());
            pause.play();
        });

        // when we just can't take it  anymore,
        // a simple click will quiet the Wumpus,
        // but you have to be quick...
        cowerInFear.setOnMouseClicked(event -> {
            timer.cancel();
            Platform.exit();
        });

        stage.setScene(new Scene(cowerInFear));
    }

    // it's so scary...
    public class WumpusAttack extends TimerTask {
        private String[] attacks = {
                "hugs you",
                "reads you a bedtime story",
                "sings you a lullaby",
                "puts you to sleep"
        };

        // the restaurant at the end of the universe.
        private Random random = new Random(42);

        @Override
        public void run() {
            // use runlater when we mess with the scene graph,
            // so we don't cross the streams, as that would be bad.
            Platform.runLater(() -> {
                cowerInFear.setText("The Wumpus " + nextAttack() + "!");
                mainStage.sizeToScene();
                mainStage.show();
            });
        }

        private String nextAttack() {
            return attacks[random.nextInt(attacks.length)];
        }
    }

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

更新,2020 年 1 月

Java 9 添加了一个名为 Platform.startup(),您可以使用它来触发 JavaFX 运行时的启动,而无需定义从 Application 派生的类和在其上调用 launch().Platform.startup()launch() 方法有类似的限制(你不能多次调用 Platform.startup()),所以如何应用它的元素类似于本答案中的 launch() 讨论和 Wumpus 示例.

Java 9 added a new feature called Platform.startup(), which you can use to trigger startup of the JavaFX runtime without defining a class derived from Application and calling launch() on it. Platform.startup() has similar restrictions to the launch() method (you cannot call Platform.startup() more than once), so the elements of how it can be applied is similar to the launch() discussion and Wumpus example in this answer.

有关如何使用 Platform.startup() 的演示,请参阅 Fabian 对 How to实现JavaFX和非JavaFX交互?

For a demonstration on how Platform.startup() can be used, see Fabian's answer to How to achieve JavaFX and non-JavaFX interaction?

这篇关于如何在java中多次调用launch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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