大佬教程收集整理的这篇文章主要介绍了Java GUI显示网页并返回HTML,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是一个使用JavaFX的人为示例,该示例将HTML内容打印到System.out
-适应创建getHTML()
方法应该不会太复杂。(我已经用JavaFX 8对其进行了测试,但是它也应该可以与JavaFX 2一起使用)。
每次加载新页面时,该代码都会打印HTML内容。
注意:我已printdocument
从此答案中借用了代码。
public class TestFX extends Application {
@OverrIDe
public voID start(Stage stagE) throws Exception {
try {
final WebVIEw webVIEw = new WebVIEw();
final WebENGIne webENGIne = webVIEw.getENGIne();
Scene scene = new Scene(webVIEw);
stage.setScene(scenE);
stage.setWIDth(1200);
stage.setHeight(600);
stage.show();
webENGIne.getLoaDWorker().stateproperty().addListener(new changelistener<Worker.State>() {
@OverrIDe
public voID changed(ObservableValue<? extends State> ov, State t, State t1) {
if (t1 == Worker.State.SUCCEEDED) {
try {
printdocument(webENGIne.getdocument(), System.out);
} catch (Exception E) { e.printstacktrace(); }
}
}
});
webVIEw.getENGIne().load("http://www.Google.com");
} catch (Exception E) {
e.printstacktrace();
}
}
public static voID printdocument(document doc, OutputStream out) throws IOException, transformerException {
transformerFactory tf = transformerFactory.newInstance();
transformer transformer = tf.newtransformer();
transformer.setoutputProperty(OutputKeys.OMIT_XML_DECLaraTION, "no");
transformer.setoutputProperty(OutputKeys.METHOD, "xml");
transformer.setoutputProperty(OutputKeys.INDENT, "yes");
transformer.setoutputProperty(OutputKeys.ENCoding, "UTF-8");
transformer.setoutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMsource(doC), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}
public static voID main(String[] args) {
launch(args);
}
}
我需要如下工作流程:
// load xyz.com in the browser window
// the browser is live,meaning users can interact with it
browser.load("http://www.google.com");
// return the HTML of the initially loaded page
String page = browser.getHTML();
// after some time
// user might have navigated to a new page,get HTML again
String newpage = browser.getHTML();
令我惊讶的是,这与Java GUI(例如JavaFX(http://lexandera.com/2009/01/extracTing-html-from-
a-webview/))和Swing 有多么困难。
有一些简单的方法可以在Java中获得此功能吗?
以上是大佬教程为你收集整理的Java GUI显示网页并返回HTML全部内容,希望文章能够帮你解决Java GUI显示网页并返回HTML所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。