【问题标题】:How to embed HtmlUnit web client at JEditorPane?如何在 JEditorPane 中嵌入 HtmlUnit Web 客户端?
【发布时间】:2015-04-10 21:20:50
【问题描述】:

我知道 JEditorPane 不能很好地呈现 Web 元素。因此我尝试了 HtmlUnit。但是,我希望将支持 JS 的浏览器嵌入到 JEditorPane 中以查看结果,并且 JEditorPane 的 .setPage() 不接受 HTML 页面而是 URL。我在一个 Javax 应用程序上。我该如何解决这个问题?

顺便说一句,稍后我需要通过 D3 将可视数据嵌入到浏览器中。感谢您提供的所有建议。

这是我的代码 sn-p:

    webclient = new WebClient (BrowserVersion.CHROME);  
    currentpage = (HtmlPage) webclient.getPage("http://www.stackoverflow.com");
    currentpage.executeJavaScript("document.write('Hello World!');");

    jepuser = new JEditorPane();
    jepuser.setEditable(false);

    try{
        jepuser.setPage(currentpage);           //<---
        jepuser.setContentType("text/html");
        jepuser.updateUI();
    }
    catch(IOException e){
        System.err.println(e);
    }
    spuser = new JScrollPane(jepuser);
    spuser.setViewportBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    spuser.setSize(800, 420);
    spuser.setLocation(280, 140);
    spuser.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    //spuser.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    spuser.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    add(spuser, BorderLayout.CENTER);

【问题讨论】:

    标签: java htmlunit jeditorpane


    【解决方案1】:

    JEditorPane 可以渲染一些基本的 HTML(可能是 3.2 版左右) HtmlUnit 是一个无头浏览器,主要用于单元测试

    您似乎想在您的 Java 应用程序中嵌入一个功能齐全的浏览器。我会推荐尝试 JavaFX - 它有一个基于 WebKit 的本机浏览器控件 - WebView

    【讨论】:

    • 谢谢。但是我怎样才能继续以任何方式运行上述程序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多