简体   繁体   English

Java Swing 项目中的 JavaFX WebView

[英]JavaFX WebView in Java Swing Project

i try to use the WebView in my java project, in my code is:我尝试在我的 java 项目中使用 WebView,在我的代码中是:

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/");

but the this thown a exception但这个例外

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

And yes, this is not a JavaFx application.是的,这不是 JavaFx 应用程序。

You can embed JavaFX content into a Swing application using JFXPanel .您可以使用JFXPanel将 JavaFX 内容嵌入到 Swing 应用程序中。 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(...) .请注意,为了使其正常工作,您必须小心地在 AWT 事件调度线程上创建和访问 Swing 内容,并在 FX 应用程序线程上创建和访问 JavaFX 内容,因此您需要使用SwingUtilities.invokeLater(...)仔细管理代码SwingUtilities.invokeLater(...)Platform.runLater(...) (See the documentation for more details.) (有关更多详细信息,请参阅文档。)

Creating a JFXPanel starts the FX Application toolkit, if it is not already started.创建JFXPanel启动 FX 应用程序工具包(如果尚未启动)。

Here is a simple example of embedding a JavaFX WebView into a Swing application:下面是一个将 JavaFX WebView嵌入到 Swing 应用程序的简单示例:

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);
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM