0
由於這是我的第一篇文章,請原諒任何不準確的地方。我的問題,我想從一個JavaScript函數傳遞價值的JavaFX在初始化方法將值從javascript傳遞給JavaFX
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
final URL urlGoogleMaps = getClass().getResource("googlemaps.html");
System.out.println("fjdsoij");
browser.getEngine().load(urlGoogleMaps.toExternalForm());
WebEngine webEngine = browser.getEngine();
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>(){
@Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if(newState == State.SUCCEEDED){
JSObject window = (JSObject)webEngine.executeScript("window");
window.setMember("app", new JavaApplication());
}
}
});
}
public class JavaApplication{
public void calljavascript(int lengthInMeters){
System.out.println(lengthInMeters);
}
}
}
和JavaScript的部分是這樣的
function initialize() {
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(0, -180),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, 157.821856)
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
//geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
var lengthInMeters = google.maps.geometry.spherical.computeLength(flightPath.getPath());
flightPath.setMap(map);
alert("polyline is "+lengthInMeters+" long");
app.calljavascript(lengthInMeters);
}
google.maps.event.addDomListener(window, 'load', initialize);
我想要的價值關於callJavascript方法中的lengthInMeters是否有任何想法?日Thnx提前
yap那是正確的解決方案現在一切正常,謝謝你:) – n00bfighter 2014-11-03 21:59:58
這不適用於隨後的加載調用。在加載之前無法設置成員。有更好的解決方案嗎? – mohamnag 2016-02-29 23:11:10
請發表一個新的問題引用這個解決方案 – 2016-03-01 08:04:18