Java jp2launcher.exe不会随小程序关闭而退出
如果我使用JavaFX内容关闭小程序(因此小程序使用EDT和JavaFX线程),jp2launcher.exe将继续运行近1分钟,这样小程序就无法轻松再次启动(一旦它未被识别为新实例–在浏览器关闭后等) 我搜索过谷歌,但没有找到解决方案。我只发现了非常类似的问题 另一种解决方案是,小程序可以在持久的jp2launcher.exe上启动,但不能。它根本不被调用。只有JApplet的init方法被重写Java jp2launcher.exe不会随小程序关闭而退出,java,swing,javafx,japplet,jfxpanel,Java,Swing,Javafx,Japplet,Jfxpanel,如果我使用JavaFX内容关闭小程序(因此小程序使用EDT和JavaFX线程),jp2launcher.exe将继续运行近1分钟,这样小程序就无法轻松再次启动(一旦它未被识别为新实例–在浏览器关闭后等) 我搜索过谷歌,但没有找到解决方案。我只发现了非常类似的问题 另一种解决方案是,小程序可以在持久的jp2launcher.exe上启动,但不能。它根本不被调用。只有JApplet的init方法被重写 import javax.swing.JApplet; import javax.swing.Sw
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.awt.Graphics;
import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.animation.Timeline;
/*<applet code="sample" width=600 height=600></applet>*/
public class sample extends JApplet{
protected Scene scene;
protected Group group;
Timeline timeline;
JFXPanel fxPanel;
@Override
public final void init(){initSwing();}
private void initSwing(){
fxPanel = new JFXPanel();
add(fxPanel);
Platform.runLater(() ->{initFX(fxPanel);});
}
private void initFX(JFXPanel fxPanel){
timeline=new Timeline();
group=new Group();
scene=new Scene(group);
}
@Override
public void start(){
try{SwingUtilities.invokeAndWait(this::initSwing);}
catch(java.lang.InterruptedException|java.lang.reflect.InvocationTargetException e){}}
}
import javax.swing.JApplet;
导入javax.swing.SwingUtilities;
导入java.awt.Graphics;
导入javafx.embed.swing.JFXPanel;
导入javafx.application.Platform;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.animation.Timeline;
/**/
公共类示例扩展了JApplet{
保护现场;
保护群;
时间线;
JFXPanel;
@凌驾
公共最终void init(){initSwing();}
私有void initSwing(){
fxPanel=新的JFXPanel();
添加(fxPanel);
runLater(()->{initFX(fxPanel);});
}
私有void initFX(JFXPanel fxPanel){
时间线=新时间线();
组=新组();
场景=新场景(组);
}
@凌驾
公开作废开始(){
试试{SwingUtilities.invokeAndWait(this::initSwing);}
catch(java.lang.InterruptedException | java.lang.reflect.InvocationTargetException e){}
}
根据您的更新
- 我无法在显示的平台上重现问题;选择退出小程序和返回命令提示符之间的延迟没有明显增加。如果问题是特定于平台的,我已经将示例作为测试参考
$ javac sample.java ; appletviewer sample.java
- 注意,“在小程序中,必须使用
从invokeAndWait
方法启动GUI创建任务”init
为时已晚applet::start
- 不习惯丢弃异常,当
为空或未初始化时,我会在JFXPanel
上看到quit
java.lang.IllegalStateException
导入javafx.embed.swing.JFXPanel;
导入javafx.application.Platform;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javax.swing.JApplet;
导入javax.swing.SwingUtilities;
/**/
公共类示例扩展了JApplet{
保护现场;
保护群;
JFXPanel;
@凌驾
公共最终void init(){
试一试{
invokeAndWait(this::initSwing);
}catch(java.lang.InterruptedException | java.lang.reflect.InvocationTargetException e){
e、 printStackTrace(系统输出);
}
}
私有void initSwing(){
fxPanel=新的JFXPanel();
添加(fxPanel);
Platform.runLater(()->{
initFX(fxPanel);
});
}
私有void initFX(JFXPanel fxPanel){
组=新组();
group.getChildren().add(新标签(
System.getProperty(“os.name”)+“v”
+System.getProperty(“os.version”)+“JavaV”
+System.getProperty(“java.version”);
场景=新场景(组);
fxPanel.setScene(场景);
}
}
请编辑您的问题,将其包括在内;请注意,这也是小程序所必需的。我对这种行为做了一些合理的长样本,但由于其短,您可以看到jp2launcher.exe终止时几乎没有延迟,但肯定不会随小程序终止而终止。
import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
/*<applet code="sample" width=300 height=200></applet>*/
public class sample extends JApplet {
protected Scene scene;
protected Group group;
JFXPanel fxPanel;
@Override
public final void init() {
try {
SwingUtilities.invokeAndWait(this::initSwing);
} catch (java.lang.InterruptedException | java.lang.reflect.InvocationTargetException e) {
e.printStackTrace(System.out);
}
}
private void initSwing() {
fxPanel = new JFXPanel();
add(fxPanel);
Platform.runLater(() -> {
initFX(fxPanel);
});
}
private void initFX(JFXPanel fxPanel) {
group = new Group();
group.getChildren().add(new Label(
System.getProperty("os.name") + " v"
+ System.getProperty("os.version") + "; Java v"
+ System.getProperty("java.version")));
scene = new Scene(group);
fxPanel.setScene(scene);
}
}