问题描述
这是我用来在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
or NativeSwing
代码段:
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://www.google.com");
@see SimpleWebBrowserExample.java
这篇关于使用JEditorPane的浏览器强制显示蓝色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!