您好,您知道如何从html(webview)获取字符串吗?我的意思是我需要在点击按钮或矩形或多边形后得到字符串值(文本):
Green building is clicked - SVG
.building {
fill: #494547;
}
.building:hover{
fill: #8c8a8b;
transition: .6s fill
}
.building:active{
fill: green;
transition: .0s
}
text{
pointer-events: none;
}
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="-99 0 600 600" style="enable-background:new -99 0 600 600;" xml:space="preserve">
<rect class="building" id="XMLID_394_" x="159" y="21" width="99" height="80.3"/>
<text id="XMLID_395_" transform="matrix(1 0 0 1 186.8921 65.6667)" class="st1 st2 st3">BN5</text>
<polygon class="building" id="XMLID_396_" points="289.5,20 321.6,20 321.6,57 397.4,57 397.4,21.8 430.8,21.8 430.8,93.8 289.5,93.8 "/>
<text id="XMLID_402_" transform="matrix(1 0 0 1 332.2969 81.5068)" class="st1 st2 st3">PK11</text>
</svg>
正如你所看到的......我需要从这个多边形文本中获取它意味着在点击多边形/路径/矩形后,我将通过鼠标点击一些方法,例如通过mouseListener点击。
如图所示,通过点击BN5建筑物,我将获得字符串BN5。
答案 0 :(得分:1)
按照WebEngine
documentation中的部分&#34;从Javascript&#34;回调Java,您可以将Javascript处理程序添加到SVG元素:
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="-99 0 600 600" style="enable-background:new -99 0 600 600;" xml:space="preserve">
<rect class="building" onclick="javaApp.process('BN5')" id="XMLID_394_" x="159" y="21" width="99" height="80.3"/>
<text id="XMLID_395_" transform="matrix(1 0 0 1 186.8921 65.6667)" class="st1 st2 st3">BN5</text>
<polygon class="building" onclick="javaApp.process('PK11')" id="XMLID_396_" points="289.5,20 321.6,20 321.6,57 397.4,57 397.4,21.8 430.8,21.8 430.8,93.8 289.5,93.8 "/>
<text id="XMLID_402_" transform="matrix(1 0 0 1 332.2969 81.5068)" class="st1 st2 st3">PK11</text>
</svg>
现在,当您加载HTML时,您可以执行以下操作:
class JSCallback {
public void process(String building) {
// do something here...
System.out.println(building);
}
}
和
WebView webView = ...;
WebEngine engine = webView.getEngine();
engine.documentProperty().addListener((obs, oldDoc, newDoc) -> {
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("javaApp", new JSCallback());
});
engine.load(...);
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
public class WebViewCallbackExample extends Application {
private final String html = "<!DOCTYPE html>"
+ "<html>"
+ "<head>"
+ "<style>"
+ ".building {"
+ " fill: #494547;"
+ "}"
+ ".building:hover{"
+ " fill: #8c8a8b;"
+ " transition: .6s fill"
+ "}"
+ ".building:active{"
+ " fill: green;"
+ " transition: .0s"
+ "}"
+"text{"
+" pointer-events: none;"
+"}"
+ "</style>"
+ "</head>"
+ "<body>"
+ "<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" viewBox=\"-99 0 600 600\" style=\"enable-background:new -99 0 600 600;\" xml:space=\"preserve\">"
+ "<rect class=\"building\" onclick=\"javaApp.process('BN5')\" id=\"XMLID_394_\" x=\"159\" y=\"21\" width=\"99\" height=\"80.3\"/>"
+ "<text id=\"XMLID_395_\" transform=\"matrix(1 0 0 1 186.8921 65.6667)\" class=\"st1 st2 st3\">BN5</text>"
+ "<polygon class=\"building\" onclick=\"javaApp.process('PK11')\" id=\"XMLID_396_\" points=\"289.5,20 321.6,20 321.6,57 397.4,57 397.4,21.8 430.8,21.8 430.8,93.8 289.5,93.8 \"/>"
+ "<text id=\"XMLID_402_\" transform=\"matrix(1 0 0 1 332.2969 81.5068)\" class=\"st1 st2 st3\">PK11</text>"
+ "</svg>"
+ "</body>"
+ "</html>" ;
@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.documentProperty().addListener((obs, oldDoc, newDoc) -> {
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("javaApp", new JSCallback());
});
engine.loadContent(html);
Scene scene = new Scene(webView, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static class JSCallback {
public void process(String building) {
// do something here...
System.out.println(building);
}
}
public static void main(String[] args) {
launch(args);
}
}