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

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

问题描述

我似乎无法找到这样的东西 - 除了它不能被嵌入在Web视图

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

目前,我有它通过一个applet从HTML页面访问和通过JavaScript控制的图像观看产品。

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

我研究使用JavaFX客户端应用程序,它可能会需要访问该小程序。我试图把它嵌入到网页视图,并没有工作。搜索本网站上表示,网页流量不支持插件技术。这是建立与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?

谢谢!

推荐答案

一个快速的试验用一个非常简单的示例小程序,​​证明它是可以嵌入回转小程序到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).

样品

下面是从甲骨文开始使用小程序文档的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上述code是绝对必要的,我只是坚持它在那里以防万一,我知之甚少的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.

建议

现有的大多数小程序是相当小的 - 它可能是一个更好的主意,重新实现一个applet作为一个纯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.

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

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