gpt4 book ai didi

java - 如何在java中多次调用launch()

转载 作者:行者123 更新时间:2023-12-01 19:06:03 26 4
gpt4 key购买 nike

如何在java中多次调用launch(),我得到一个异常“ERROR IN MAIN:java.lang.IllegalStateException:应用程序启动不得被调用多次”

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

JavaFx 应用程序代码:

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 应用程序

// 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 应用程序上运行是不允许的。

来自javadoc:

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

定期显示窗口的建议

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

有关此方法的简短摘要实现:

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

Wumpus 示例

这个示例比实际需要的要复杂一些,因为它还涉及计时器任务。不过,它确实提供了一个完整的独立示例,有时可能会有所帮助。

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 示例类似。

有关如何使用 Platform.startup() 的演示,请参阅 Fabian 对 How to achieve JavaFX and non-JavaFX interaction? 的回答。

关于java - 如何在java中多次调用launch(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59554211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com