1

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

我搜索了谷歌,但我没有找到解决方案。我只发现了非常相似的问题——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){}}  
}
4

1 回答 1

1

根据您的更新,

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

    $ javac sample.java ; appletviewer sample.java
    
  • 此处指出,“在小程序中,必须从init使用 的方法启动 GUI 创建任务invokeAndWait。” Applet::start为时已晚。

  • 不习惯丢弃异常,我看到java.lang.IllegalStateExceptionquit何时JFXPanel为空或未初始化。

图片

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);
    }
}
于 2016-05-04T20:29:21.393 回答