使用JEditorPane的浏览器强制显示蓝色背景 [英] Browser using JEditorPane forcing blue background

查看:70
本文介绍了使用JEditorPane的浏览器强制显示蓝色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来在JEditorPane中显示google的代码

This is the code I'm using to display google in a JEditorPane

String url="http://google.com";    
editorPane.setEditable(false);
    try {
        editorPane.setPage(url);
    } catch (IOException e) {}

但是由于某种原因,背景将始终是蓝色,如果我打电话也没关系

But for some reason the background will always be a blue colour, doesn't matter if I call

setBackgroundColor(Color.WHITE);

推荐答案

正如@AndrewThompson在评论中提到的 JEditorPane 确实落后,它仅支持HTML 3.2和CSS1的一个子集,并且不是真正渲染任何现代网页的电缆.

As @AndrewThompson noted in the comments JEditorPane is really behind, it supports only a subset of HTML 3.2 and CSS1, and isn't really cable of rendering any modern web pages.

我强烈建议您使用其他替代方法,例如:

I strongly suggest using an alternative, like:

  • JavaFX WebView

代码段:(无依赖关系,您可以按原样运行)

Code Snippet: (no dependencies, you can run it as-is)

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.*;

public class JavaFxBrowser implements Runnable {
    private WebEngine webEngine;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JavaFxBrowser());
    }

    public void loadURL(final String url) {
        Platform.runLater(() -> {
            webEngine.load(url);
        });
    }

    @Override
    public void run() {
        // setup UI
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setPreferredSize(new Dimension(1024, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JFXPanel jfxPanel = new JFXPanel();
        frame.getContentPane().add(jfxPanel);
        frame.pack();

        Platform.runLater(() -> {
            WebView view = new WebView();
            webEngine = view.getEngine();

            jfxPanel.setScene(new Scene(view));
        });

        loadURL("http://www.google.com");
    }
}

  • 飞碟

  • Flying Saucer

    代码示例:

    XHTMLPanel panel = new XHTMLPanel();
    panel.setDocument("http://www.google.com");
    

    @请参见 BrowsePanel.java

    NativeSwing

    or NativeSwing

    代码段:

    final JWebBrowser webBrowser = new JWebBrowser();
    webBrowser.navigate("http://www.google.com");
    

    @see 查看全文

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