JavaFx Webview不会加载网址

时间:2015-07-10 07:03:04

标签: webview javafx load jpanel

我遵循了很多教程,但也许我错过了一些东西,因为网址没有加载到javafx webview中,而是显示白色面板,而不是网页。我尝试使用http://google.com和其他网页,但都没有显示。 下面的JPanel出现在JTabbedPane中。我认为白色面板是JFXPanel,填充如下。

这是代码:

public class RtcOverview extends JPanel {

private final JFXPanel jfxPanel = new JFXPanel();
private WebEngine engine;

String url = "http://google.com";

public RtcOverview() {

    super();
    initComponents();
    this.add(jfxPanel);
}

private void initComponents() {


    Platform.runLater(new Runnable() {
        @Override
        public void run() {

            WebView view = new WebView();

            engine = view.getEngine();
            engine.load(url);
            Scene scene = new Scene(view);
            jfxPanel.setScene(scene);
        }
    });
}

enter image description here

1 个答案:

答案 0 :(得分:4)

对我来说非常适合。我正在附加MCVE,以便您可以比较并查看您的情况出了什么问题。如果您的机器上的MCVE出现故障,我建议您检查您的互联网设置,并确保您的设置不会阻止该应用程序使用互联网。

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

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main extends JFrame {

    private final JFXPanel jfxPanel = new JFXPanel();
    private WebEngine engine;

    String url = "http://google.com";

    public Main() {
        super();
        initComponents();
        getContentPane().add(jfxPanel);
        setSize(500, 500);
        // Kill everything on closing the frame
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    private void initComponents() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                WebView view = new WebView();
                engine = view.getEngine();
                engine.load(url);
                Scene scene = new Scene(view);
                jfxPanel.setScene(scene);
            }
        });
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.setVisible(true);
    }
}