【问题标题】:jp2launcher.exe does not exits with applet closurejp2launcher.exe 不退出小程序关闭
【发布时间】:2016-05-03 06:22:45
【问题描述】:

如果我关闭带有 JavaFX 内容的小程序(因此小程序正在使用 EDT 和 JavaFX 线程),jp2launcher.exe 将继续运行近 1 分钟,因此小程序无法再次轻松启动(一旦它未被识别为新实例 -浏览器关闭后等)。

我在 Google 上搜索过,但没有找到解决方案。我只发现了非常相似的问题——https://bugs.openjdk.java.net/browse/JDK-8051030

另一个解决方案是,如果小程序可以在持久的 jp2launcher.exe 上启动,但它不能。它根本没有被调用。只覆盖了 JApplet 的 init 方法。

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){}}  
}

【问题讨论】:

  • 请编辑您的问题以包含minimal reproducible example;请注意,小程序也需要this
  • 我已经对此行为做了一些合理的长示例,但是由于它的简短性,您可以看到 jp2launcher.exe 以非常短的延迟终止,但绝对不会随着小程序终止而终止.

标签: java swing javafx japplet jfxpanel


【解决方案1】:

根据您的更新,

  • 我无法在所示平台上重现问题;选择退出小程序和返回命令提示符之间的延迟没有明显增加。如果问题是特定于平台的,我已将示例包含在测试中以供参考。

    $ javac sample.java ; appletviewer sample.java
    
  • 注意到here,“在小程序中,必须使用invokeAndWaitinit 方法启动GUI 创建任务。” Applet::start 来不及了。

  • 不习惯丢弃异常,当JFXPanel 为空或未初始化时,我在quit 上看到java.lang.IllegalStateException

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

【讨论】:

    猜你喜欢
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    相关资源
    最近更新 更多