将 JavaFX 2.0 WebView 集成到 Swing Java SE 6 应用程序中 [英] Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application

查看:38
本文介绍了将 JavaFX 2.0 WebView 集成到 Swing Java SE 6 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将 Web 浏览器组件集成到现有 Swing 应用程序中的方法,并找到了 WebView 用于 Java FX 2.0.此外,我在 java.net 上发现了一篇博客文章,显示 如何将 Java FX 组件集成到 Swing 应用程序中.所以我想这可能是可行的,但我还没有尝试过.

I was looking for a way to integrate a Web-Browser-Component in an existing Swing-Application and found WebView for Java FX 2.0. Furthermore I found a blog post on java.net showing how to integrate a Java FX component into a Swing Application . So I guess it might be doable, but I haven't tried yet.

我很好奇,你认为这是一个好方法吗?有没有更好的解决方案?它甚至可行吗?可能是预先捆绑了一些东西吗?

I'm curious, do you think this is a good approach? Are there any better solutions? Is it even doable? Is maybe something prebundled out there?

动机是:我想将一些 WebBrowser-whatever 集成到现有的 Swing-Application 中,长期目标是摆脱整个 Java 桌面完全应用程序,将其替换为基于 Web 的解决方案(计划是将现有方面慢慢转换为网页,然后显示在 WebBrowser-Component 中,直到 Swing 应用程序除了浏览器框架之外什么都没有).后端当然仍然是 Java :-)

The motivation is: I want to integrate some WebBrowser-whatever into an existing Swing-Application, the long-term goal being to get rid of the whole Java Desktop Application at all, replacing it with a web-based solution (the plan is to slowly convert existing aspects into webpages which are then displayed in the WebBrowser-Component until nothing is left of the swing application except for the browser-skeleton). The backend of course remains Java :-)

我还没有尝试过,因为我只是没有时间将 JavaFX 与我的项目集成(这是一份工作,从长远来看,我们只是在探索替代方案),所以我最好在被烧毁之前询问.

I haven't tried yet since I simply lack the time to integrate JavaFX with my project (its a job, we're just exploring alternatives fpr the long run), so I better ask before I get burned.

推荐答案

很有可能!

必须安装 JavaFX 2.0,然后设法安装 jfxrt.jar 在类路径中.

One has to install JavaFX 2.0, and somehow manage to have jfxrt.jar in the Classpath.

以下代码呈现一个 JFXPanel在 JFrame 中.JFXPanel 包含一个 WebView 加载>google.com.

The following code renders a JFXPanel inside a JFrame. The JFXPanel contains a WebView which loads google.com.

但是,至少在我的机器上,WebView 感觉相当草率.我在 Mac OS X 10.6 上工作;JavaFX 2.0 仍处于 OS X 的测试阶段.

However, at least on my machine, the WebView feels rather sloppy. I'm working on Mac OS X 10.6; JavaFX 2.0 is still in beta for OS X.

替代方案我发现包括 MozSwing,它看起来非常有前途并且感觉实际上相当快.遗憾的是,该项目自 2008 年以来就没有进一步开发,而且捆绑的 XUL 运行器相当陈旧(没有新的花哨的 html 5).

Alternatives I found include MozSwing, which looked very promising and feels quite fast actually. Sadly the project is not being developed any further since 2008 and the bundled XUL runner is rather old (no new fancy html 5).

通过 Maven 包含这两种方法都是一场噩梦,您最好设置自己的本地存储库.

Both approaches are a nightmare to include via maven, you better setup your own local repository.

import java.awt.Dimension;
import java.awt.Point;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JavaFX {

    /* Create a JFrame with a JButton and a JFXPanel containing the WebView. */
    private static void initAndShowGUI() {
        // This method is invoked on Swing thread
        JFrame frame = new JFrame("FX");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().setLayout(null); // do the layout manually

        final JButton jButton = new JButton("Button");
        final JFXPanel fxPanel = new JFXPanel();

        frame.add(jButton);
        frame.add(fxPanel);
        frame.setVisible(true);

        jButton.setSize(new Dimension(200, 27));
        fxPanel.setSize(new Dimension(300, 300));
        fxPanel.setLocation(new Point(0, 27));

        frame.getContentPane().setPreferredSize(new Dimension(300, 327));
        frame.pack();
        frame.setResizable(false);

        Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
            @Override
            public void run() {
                initFX(fxPanel);
            }
        });
    }

    /* Creates a WebView and fires up google.com */
    private static void initFX(final JFXPanel fxPanel) {
        Group group = new Group();
        Scene scene = new Scene(group);
        fxPanel.setScene(scene);

        WebView webView = new WebView();

        group.getChildren().add(webView);
        webView.setMinSize(300, 300);
        webView.setMaxSize(300, 300);

            // Obtain the webEngine to navigate
        WebEngine webEngine = webView.getEngine();
        webEngine.load("http://www.google.com/");
    }

    /* Start application */
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}

这篇关于将 JavaFX 2.0 WebView 集成到 Swing Java SE 6 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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