- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在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.
定期显示窗口的建议
Application.launch()
即可。Platform.setImplicitExit(false)
让 JavaFX 运行时在后台运行,这样当您隐藏最后一个应用程序窗口时,JavaFX 不会自动关闭。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/
运行真机调试时,出现如下问题。 Could not launch “MY-APP-NAME” iPhone has denied the launch request. Internal launch
每次单击“运行方式”按钮时,都会收到以下消息。 “选品无法推出,近期也没有推出。” 如果我选择“运行方式”按钮附近的下拉箭头,然后选择“新配置”选项(这是我设置为使用本地主机启动的配置)......它
启动过去有效的python文件时出现上述错误。我想我知道这是怎么发生的,但不确定如何解决。我有两台计算机双启动不同的操作系统,并使用保管箱保持它们之间的同步。过去,我以这种方式将我的Eclipse工作
我尝试使用 Eclipse 构建并运行“Hello world”,但它无法运行。我不使用 Android 或其他任何纯 C++。 http://i57.tinypic.com/x51h5h.png 最
这个问题在这里已经有了答案: "Selection cannot be launched and there are no recent launches” when Eclipse for Andr
在 Kotlin 中有多种启动协程的方法。我发现了几个示例,其中 GlobalScope 和 CoroutineScope 被使用。但是后者是在启动协程时直接创建的: 使用 GlobalScope :
我正在使用 10gen 版本在 OS X 上安装 MongoDB。 但是their installation tutorial事实证明对我来说有点稀缺。 到目前为止,我已经找到了安装为 launch
我在互联网上到处寻找,并尝试了论坛所说的一切,但没有任何效果。这个错误不断出现。我试过运行我的java项目(不适用于android),即使下拉运行按钮不起作用,因为它说“不适用”。 最佳答案 Ecli
我理解 std::async 对以下参数的作用。 std::launch::async std::launch::deferred 然而,std::launch::async | 会发生什么? std
我正在尝试使用 Eclipse 打开一个解压缩的 Android 项目,但每次我尝试运行或调试时都会显示此错误:“无法启动选择并且最近没有启动”。 所以我查找并尝试从现有源创建一个新项目,但打开时出现
我删除了 React Native 创建的默认启动屏幕文件,而是创建了一个启动图像。当我在模拟器中运行应用程序时,会显示启动图像,但是当我存档项目并通过 Testflight 打开它时,它仍然显示旧的
从深层链接(具有应用特定方案的 URL)启动我的 iOS 应用时,我在日志中收到此错误: lsd[738] : LaunchServices: application launch failed -
当我使用 visual studio code 调试 nodejs 应用程序时。visual studio code 告诉我 request 'launch': cannot launch targe
我创建了 Cocoa 应用程序,它是启动代理的类型。为了分发这个,我还创建了一个包,它安装该应用程序并将 launchagnet plist 文件复制到/Libraries/LaunchAgents
大家好,我今天在开发的 Java Swing 应用程序中发现了一个很大的疑问。 我发现的问题是launch() 当我在本地尝试我开发的应用程序时,它运行时没有任何延迟,但是当我使用 jnlp 从我的
我正在尝试使用 JavaFX 设置视频在 JFrame 中播放。 但是一旦我调用 launch() 函数来设置 JavaFX 并播放视频,我就会收到以下错误 应用程序构造函数中出现异常线程“AWT-E
我试图在 Android Studio 的外部工具中设置 javah 命令,但我得到了 IllegalArgumentException: Not a valid class name: Files\
我正在尝试创建一个简单的应用程序。我创建了一个新的 AVD。我启动了它。等了一个小时,模拟器只显示ANDROID。我卡住了。有人可以帮忙吗。 一个小时的样子请不要标记重复。我尽可能多地搜索和尝试。 我
进程文件: launch or launch.exe 进程名称: Vantarakis Launchh 进程类别:存在安全风险的进程 英文描述: launch.exe is a part o
将 google chrome 更新到最新版本 89.0.4389.82 (Official Build) (64-bit) 后开始出现错误。操作系统:Windows 10 错误: Puppeteer
我是一名优秀的程序员,十分优秀!