Java项目中的JavaFx WebView [英] JavaFx WebView in Java Project

查看:1915
本文介绍了Java项目中的JavaFx WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的java项目中使用WebView,在我的代码中是:

i try to use the WebView in my java project, in my code is:

JFXPanel fxPanel = new JFXPanel();
fxPanel.setBounds(10, 48, 439, 362);
desktopPane.add(fxPanel);

WebView webView = new WebView();
fxPanel.setScene(new Scene(webView));
webView.getEngine().load("http://www.stackoverflow.com/");

但这会引发异常

java.lang.IllegalStateException: Not on FX application thread; currentThread = main

是的,这不是JavaFx应用程序。

And yes, this is not a JavaFx application.

推荐答案

您可以使用 JFXPanel 。请注意,要使其正常工作,您必须小心在AWT事件分派线程上创建和访问Swing内容,并在FX应用程序线程上创建和访问JavaFX内容,因此您需要使用 SwingUtilities.invokeLater(...) Platform.runLater(...)。 (有关更多信息,请参阅文档详细信息。)

You can embed JavaFX content into a Swing application using JFXPanel. Note that for this to work correctly, you have to be careful to create and access the Swing content on the AWT event dispatch thread, and create and access the JavaFX content on the FX Application Thread, so you will need to carefully manage code using SwingUtilities.invokeLater(...) and Platform.runLater(...). (See the documentation for more details.)

创建 JFXPanel 启动FX Application工具包(如果尚未启动)。

Creating a JFXPanel starts the FX Application toolkit, if it is not already started.

以下是将JavaFX WebView 嵌入Swing应用程序的简单示例:

Here is a simple example of embedding a JavaFX WebView into a Swing application:

import java.awt.BorderLayout;

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

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

public class FXWebViewInSwing {

    private JFXPanel jfxPanel ;

    public void createAndShowWindow() {
        JFrame frame = new JFrame();
        JButton quit = new JButton("Quit");
        quit.addActionListener(event -> System.exit(0));
        jfxPanel = new JFXPanel();
        Platform.runLater(this::createJFXContent);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(quit);

        frame.add(BorderLayout.CENTER, jfxPanel);
        frame.add(BorderLayout.SOUTH, buttonPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,  800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void createJFXContent() {
        WebView webView = new WebView();
        webView.getEngine().load("http://stackoverflow.com/questions/42297864/javafx-webview-in-java-project");
        Scene scene = new Scene(webView);
        jfxPanel.setScene(scene);
    }

    public static void main(String[] args) {
        FXWebViewInSwing swingApp = new FXWebViewInSwing();
        SwingUtilities.invokeLater(swingApp::createAndShowWindow);
    }
}

这篇关于Java项目中的JavaFx WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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