是否可以在 JavaFX 应用程序(桌面)中使用现有的 Applet [英] Is it possible to use an existing Applet within a JavaFX application (desktop)

查看:42
本文介绍了是否可以在 JavaFX 应用程序(桌面)中使用现有的 Applet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到任何关于它的东西 - 除了它不能嵌入到网络视图中.

I can't seem to find anything on this - other than it can't be embedded in a web view.

我目前有一个图像查看产品,它可以通过小程序访问并通过 JavaScript 从 HTML 页面进行控制.

I currently have an image viewing product which is accessed via an applet and controlled via JavaScript from an HTML page.

我正在调查一个使用 JavaFX 的客户端应用程序,它可能需要访问该小程序.我试图将它嵌入到 WebView 中,但没有奏效.在这个网站上搜索说webview不支持插件技术.这是使用 Java FX 构建小程序 - 而是调用现有产品并与之交互.

I am investigating a client application using JavaFX and it would likely need to access that applet. I attempted to embed it into the WebView and that didn't work. Searching on this site stated that webview doesn't support plug in technology. This is to build an applet with Java FX - but rather to invoke and interact with an existing product.

因此我想知道是否还有另一种方法 - 使用 JavaFX 8?

Therefore I am left wondering if there is another way - using JavaFX 8?

谢谢!

推荐答案

一个非常简单的示例小程序的快速试用,表明可以将 Swing 小程序嵌入 JavaFX 应用程序(使用 Java 8).

A quick trial with a really simple sample applet, demonstrates that it is possible to embed Swing applets into JavaFX applications (with Java 8).

示例

这是 Oracle 小程序入门文档中的 HelloWorld 小程序:

import javax.swing.*;

public class HelloWorldApplet extends JApplet {
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

这是一个嵌入它的 JavaFX 应用程序:

And here is a JavaFX application which embeds it:

import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.awt.Dimension;

import java.util.concurrent.*;

public class JavaFXSwingAppletHolderApplication extends Application {
    private JApplet applet = new HelloWorldApplet();
    private Dimension appletSize;

    @Override public void init() throws ExecutionException, InterruptedException {
        applet.init();

        FutureTask<Dimension> sizingTask = new FutureTask<>(() ->
            applet.getRootPane().getPreferredSize()
        );
        SwingUtilities.invokeLater(sizingTask);
        appletSize = sizingTask.get();
    }

    @Override public void start(Stage stage) {
        final SwingNode swingNode = new SwingNode();
        SwingUtilities.invokeLater(() ->
            swingNode.setContent(applet.getRootPane())
        );

        stage.setScene(
            new Scene(
                new Group(swingNode),
                appletSize.getWidth(), appletSize.getHeight(),
                Color.BLACK
            )
        );
        stage.show();
    }

    @Override public void stop() {
        applet.stop();
        applet.destroy();
    }

    public static void main(String[] args) {
         launch(args);
     }
}

注意事项

我不确定 sizingTask 是否是上面的代码是绝对必要的,我只是把它放在那里以防万一,因为我对 Swing 布局知之甚少,所以认为最好是明确的.

I'm not sure if the sizingTask is the above code is strictly necessary, I just stuck it in there just in case as I know little about Swing layout, so thought it best to be explicit.

此示例仅嵌入了一个基本的小程序,您的小程序会更复杂,因此您需要为您的特定小程序验证类似的解决方案,以确保它适合您.

This sample only embeds a basic applet, your applet will be more complex so you will need to validate a similar solution for your particular applet to ensure it works for you.

推荐

大多数现有的小程序都非常小 - 将小程序重新实现为纯 JavaFX 组件实现,而不是尝试将小程序嵌入到 JavaFX 应用程序中,这可能是一个更好的主意.

Most existing applets are quite small - it is probably a better idea to reimplement an applet as a pure JavaFX component implementation rather than try to embed the applet in a JavaFX application.

另请注意,JEP 289: Deprecated the Applet 已弃用小程序 API 作为 Java 9 的一部分API.

这篇关于是否可以在 JavaFX 应用程序(桌面)中使用现有的 Applet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆