国外网站建设发展现状,wordpress的结构,广州营销推广公司,门户网站优化方案我有一段时间回过头来玩JavaFX#xff0c;并且在使用该语言方面有好有坏的经验。 随着JavaFX 2.0 beta的发布#xff0c;我想尝试一下。 在这里#xff0c;我开发了一个简单的地址解析应用程序#xff0c;该应用程序将使用Google地址编码API来获取地址并提供该位置的纬度-经… 我有一段时间回过头来玩JavaFX并且在使用该语言方面有好有坏的经验。 随着JavaFX 2.0 beta的发布我想尝试一下。 在这里我开发了一个简单的地址解析应用程序该应用程序将使用Google地址编码API来获取地址并提供该位置的纬度-经度值。 我使用Groovy进行JSON解析因为最新版本1.8提供了一个非常整洁的json解析支持。 import groovy.json.*class GeocodingParser {static def GEOCODE_JSON_URL http://maps.googleapis.com/maps/api/geocode/jsonstatic def GEOCODE_XML_URL http://maps.googleapis.com/maps/api/geocode/xmlstatic def getGeocodeForAddress(address){def queryBuilder []queryBuilder address${URLEncoder.encode(address)}queryBuilder sensorfalsedef queryString queryBuilder.join()def requestUrl GEOCODE_JSON_URL?${queryString}def payload new URL(requestUrl).textdef jsonSlurper new JsonSlurper()def doc jsonSlurper.parseText(payload)def geocode new Geocode()geocode.latitude doc.results.geometry.location.lat.join()geocode.longitude doc.results.geometry.location.lng.join()geocode.locationType doc.results.geometry.location_type.join()return geocode}
}class Geocode {def String latitudedef String longitudedef String locationTypedef String toString(){return Latitude: ${latitude}, Longitude:${longitude} and Location type: ${locationType}}
} 您可以看到使用JsonSlurper进行的json解析非常简洁。 groovy解析器返回Geocode包装器类中的纬度经度和位置类型这些是我们应用程序所关注的值的值这也是Grooy Bean。 现在让我们看一下实际上是本文重点的JavaFX代码 public class NewFXMain extends Application {/*** param args the command line arguments*/public static void main(String[] args) {Application.launch(NewFXMain.class, args);}Overridepublic void start(Stage primaryStage) {primaryStage.setTitle(Geocoder);TabPane mainTabPane new TabPane();Tab geoTab new Tab(Geocoding);geoTab.setClosable(false);mainTabPane.getTabs().add(geoTab);final GridPane geoGrid new GridPane();geoGrid.setHgap(10);geoGrid.setVgap(10);geoGrid.setPadding(new Insets(0, 20, 0, 10));Label mainGeoLabel new Label(Geocoding);final TextBox geoAddressTextBox new TextBox(15);Button geoCodeButton new Button(Geocode);final TextBox latitudeValTextBox new TextBox();latitudeValTextBox.setEditable(false);final TextBox longitudeValTextBox new TextBox();longitudeValTextBox.setEditable(false);final TextBox locationTypeValTextBox new TextBox();locationTypeValTextBox.setEditable(false);final StringProperty latitudeProperty new StringProperty();latitudeProperty.addListener(new ChangeListenerString() {Overridepublic void changed(ObservableValue? extends String observable, String oldValue, String newValue) {latitudeValTextBox.setText(newValue);}});final StringProperty longitudeProperty new StringProperty();longitudeProperty.addListener(new ChangeListenerString() {Overridepublic void changed(ObservableValue? extends String observable, String oldValue, String newValue) {longitudeValTextBox.setText(newValue);}});final StringProperty locationTypeProperty new StringProperty();locationTypeProperty.addListener(new ChangeListenerString() {Overridepublic void changed(ObservableValue? extends String observable, String oldValue, String newValue) {locationTypeValTextBox.setText(newValue);}});geoCodeButton.setOnAction(new EventHandlerActionEvent(){Overridepublic void handle(ActionEvent event) {String address geoAddressTextBox.getText();if(address null){}else{Geocode parsedCode (Geocode)GeocodingParser.getGeocodeForAddress(address);latitudeProperty.set(parsedCode.getLatitude());longitudeProperty.set(parsedCode.getLongitude());locationTypeProperty.set(parsedCode.getLocationType());}}});geoGrid.add(mainGeoLabel, 4, 1);geoGrid.add(new Label(Address), 2, 3);geoGrid.add(geoAddressTextBox, 3, 3,3,1);geoGrid.add(new Label(Latitude), 2,7);geoGrid.add(new Label(Longitude),2,8);geoGrid.add(new Label(Location Type),2,9);geoGrid.add(latitudeValTextBox,3,7,2,1);geoGrid.add(longitudeValTextBox,3,8,2,1);geoGrid.add(locationTypeValTextBox,3,9,2,1);geoGrid.add(geoCodeButton, 4, 5);geoTab.setContent(geoGrid);Scene scene new Scene(mainTabPane);primaryStage.setScene(scene);primaryStage.setVisible(true);primaryStage.setResizable(false);}
} 我已经使用绑定来绑定显示纬度经度和位置类型值的组件以及具有相同值的属性。 例如以下代码显示了纬度值如何绑定到将显示该值的控件。 该控件文本框保存了通过Geocoding API发送的json响应后获得的纬度值。 现在我们创建一个StringProperty来保存纬度的值并将更改侦听器附加到此属性以使该属性中的值一旦更新便会使用新值更新文本框。 那么到底是什么改变了这个财产的价值 我们添加一个按钮该按钮调用groovy解析器并在包装类中获取纬度经度和位置类型值。 在上面的动作侦听器中我们获取已解析的值然后使用相应的值更新属性。 此更新依次触发相应的更改侦听器中的方法。 现在进入控件的布局。 我使用了GridBox布局这非常灵活因为它使我能够以整齐的顺序放置组件。 以下是一些想法 JavaFX 2.0已更改以使Java程序员更加友好 JavaFX 2.0比JavaFX脚本更为冗长-例如研究必须完成绑定的方式。 缺乏工具支持–创建GUI很困难。 Java程序员不必一起学习新的语言他们对JavaFX API感到很宾至如归 与其他JVM语言如GroovyScala的互操作性。 JavaFX 2.0中添加了许多新控件API。 缺乏多平台支持。 源代码可以在这里找到。 参考 使用JavaFX 2.0 beta的示例应用程序以及 JCG合作伙伴 Mohamed Sanaulla在Experiences Unlimited Blog上的 想法 。 相关文章 Xuggler开发教程 YouTube Java API入门 SmartGWT入门提供出色的GWT界面 翻译自: https://www.javacodegeeks.com/2011/06/javafx-20-beta-sample-application-and.html