在这里,我开发了一个简单的地址解析应用程序,该应用程序将使用Google地址编码API来获取地址并提供该位置的纬度-经度值。
我使用Groovy进行JSON解析,因为最新版本1.8提供了一个非常整洁的json解析支持。
import groovy.json.*
class GeocodingParser {
static def GEOCODE_JSON_URL = "http://maps.googleapis.com/maps/api/geocode/json"
static def GEOCODE_XML_URL = "http://maps.googleapis.com/maps/api/geocode/xml"
static def getGeocodeForAddress(address){
def queryBuilder = []
queryBuilder << "address=${URLEncoder.encode(address)}"
queryBuilder << "sensor=false"
def queryString = queryBuilder.join("&")
def requestUrl = GEOCODE_JSON_URL+"?${queryString}"
def payload = new URL(requestUrl).text
def 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 latitude
def String longitude
def String locationType
def 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);
}
@Override
public 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 ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
latitudeValTextBox.setText(newValue);
}
});
final StringProperty longitudeProperty = new StringProperty();
longitudeProperty.addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
longitudeValTextBox.setText(newValue);
}
});
final StringProperty locationTypeProperty = new StringProperty();
locationTypeProperty.addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
locationTypeValTextBox.setText(newValue);
}
});
geoCodeButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public 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语言(如Groovy,Scala)的互操作性。
- JavaFX 2.0中添加了许多新控件,API。
- 缺乏多平台支持。
源代码可以在这里找到。
参考: 使用JavaFX 2.0 beta的示例应用程序以及 JCG合作伙伴 Mohamed Sanaulla在Experiences Unlimited Blog上的 想法 。
翻译自: https://www.javacodegeeks.com/2011/06/javafx-20-beta-sample-application-and.html