java gui 颜色从html_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() {

@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);

}

}

2020-11-16

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值