注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

Yahoo Weather for Xi'an(JavaFX2.0)  

2011-10-15 10:59:23|  分类: Javafx |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
继续接着 上一篇的《Get weather from Yahoo (Use Scala)》的内容。

做一个JavaFX 2.0 GUI (由于后台不知道Java 和 Scala 之间如何相互调用,所以后台改用Java解析)
首先说一下这个简单的小例子 (例子写的比较匆忙,Code很烂)。
因为是初次使用JavaFX,很多东西都不清楚,所以GUI很简单,就是一个VBox 所有元素都从上到下排列。

开发工具:NetBeans 7.1 Beta
JavaFX code
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package yahooweather;

import java.util.Map;
import javafx.scene.control.Label;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import yahooweather.weatherfinder.WeatherFinder;

/**
 *
 * @author hjzheng
 */
public class YahooWeather extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Yahoo Weather for Xi'an");
        VBox vbox = new VBox();
        vbox.setPadding(new Insets(10,20,20,10));
        vbox.setSpacing(10);
       
        Scene scene = new Scene(vbox, 520, 350);
      
        primaryStage.setScene(scene);
        primaryStage.show();
       
        WeatherFinder wf = new WeatherFinder();
        Map<String,String> weatherInfo = wf.getWeatherInfos("2157249");
       
        Label title = new Label(weatherInfo.get("title"));
        title.setFont(Font.font("Arial", FontWeight.BLACK, 30));
       
        Path path = new Path();
        path.getElements().add(new MoveTo(0.0f, 0.0f));
        path.getElements().add(new HLineTo(480.0f));
        path.setFill(Color.GREY);
       
        Label time = new Label(weatherInfo.get("date"));
        time.setFont(Font.font("Arial", FontWeight.BLACK, 24));
        time.setTextFill(Color.BLUE);
       
        Image img = new Image(weatherInfo.get("imagURL"), 60, 60, true, true);
        Label weatherPic = new Label("", new ImageView(img));
      
        Label currentWeather = new Label("Current Conditions:");
        currentWeather.setFont(Font.font("Arial", FontWeight.BLACK, 22));
       
        Text textForCurrentWeather = new Text();
        textForCurrentWeather.setFont(Font.font("Arial", FontWeight.MEDIUM, 22));
        textForCurrentWeather.setText(weatherInfo.get("text") +", "+ weatherInfo.get("temp") + weatherInfo.get("unit"));
       
        Label forecast = new Label("Forecast:");
        forecast.setFont(Font.font("Arial", FontWeight.BLACK, 22));
       
        Text textForForecast1 = new Text(); 
        textForForecast1.setFont(Font.font("Arial", FontWeight.MEDIUM, 22));
        textForForecast1.setText(weatherInfo.get("forecast0"));
        Text textForForecast2 = new Text();
        textForForecast2.setFont(Font.font("Arial", FontWeight.MEDIUM, 22));
        textForForecast2.setText(weatherInfo.get("forecast1"));
       
        vbox.getChildren().add(title);
        vbox.getChildren().add(path);
         vbox.getChildren().add(time);
        vbox.getChildren().add(weatherPic);
        vbox.getChildren().add(currentWeather);
        vbox.getChildren().add(textForCurrentWeather);
        vbox.getChildren().add(forecast);
        vbox.getChildren().add(textForForecast1);
        vbox.getChildren().add(textForForecast2);
    }
}

后台Java 数据来源

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package yahooweather.weatherfinder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


/**
 *
 * @author hjzheng
 */
public class WeatherFinder {
    private InputStream getWeatherXML(String cityCode) throws IOException {
        URL url = new URL("http://weather.yahooapis.com/forecastrss?w=" + cityCode + "&u=c");
        URLConnection connection = url.openConnection();
        return connection.getInputStream();
    }
   
  
   
    private Map<String, String> parseWeatherXML(String cityCode) throws Exception{
        InputStream in = getWeatherXML(cityCode);
        //使用XPath
        SAXReader saxReader = new SAXReader();   
      
        Document doc = saxReader.read(in);
        System.out.println(doc.asXML());
        Map<String, String> nameSpaceMap = new HashMap<String, String>(); 
        nameSpaceMap.put("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
        saxReader.getDocumentFactory().setXPathNamespaceURIs(nameSpaceMap);
        List<Element>  titleEle = doc.selectNodes("/rss/channel/description");
        String title = titleEle.get(0).getText();
        List<Element>  dateEle = doc.selectNodes("/rss/channel/item/title");
        String date = dateEle.get(0).getText();
       
        List<Element>  yweatherLocation = doc.selectNodes("/rss/channel/yweather:location");
        String city = yweatherLocation.get(0).attributeValue("city");
        List<Element>  yweatherUnits = doc.selectNodes("/rss/channel/yweather:units");
        String unit = yweatherUnits.get(0).attributeValue("temperature");
        List<Element>  yweatherCondition = doc.selectNodes("/rss/channel/item/yweather:condition");
        String text = yweatherCondition.get(0).attributeValue("text");
        String temp = yweatherCondition.get(0).attributeValue("temp");
      
        List<Element>  description = doc.selectNodes("/rss/channel/item/description");
        String desc = description.get(0).getText();
        String imagURL = StringUtils.substringBetween(desc.trim(), "src=\"","\"/>");
   
        List<Element>  yweatherForecast = doc.selectNodes("/rss/channel/item/yweather:forecast");
       
        String forecast0 = yweatherForecast.get(0).attributeValue("date") + " - " + yweatherForecast.get(0).attributeValue("text")+" Hight:"
        + yweatherForecast.get(0).attributeValue("high")+" Low:"+yweatherForecast.get(0).attributeValue("low");
               
        String forecast1 = yweatherForecast.get(1).attributeValue("date") + " - " + yweatherForecast.get(1).attributeValue("text")+" Hight:"
        + yweatherForecast.get(1).attributeValue("high")+" Low:"+yweatherForecast.get(1).attributeValue("low");
               
        in.close();
       
        Map<String, String> weatherMap = new HashMap<String, String>();
        weatherMap.put("date", date);
        weatherMap.put("city", city);
        weatherMap.put("temp", temp);
        weatherMap.put("text", text);
        weatherMap.put("unit", unit);
        weatherMap.put("title", title);
        weatherMap.put("imagURL", imagURL);
        weatherMap.put("forecast0", forecast0);
        weatherMap.put("forecast1", forecast1);
        return weatherMap;
       
    }
   
    public Map<String,String> getWeatherInfos(String cityCode) {
        try {
            Map<String,String> map = parseWeatherXML(cityCode);
            return map;
        } catch (Exception ex) {
            return null;
        }
    }
   
    public static void main(String[] args) throws Exception {
        WeatherFinder wf = new WeatherFinder();
        wf.parseWeatherXML("2157249");
    }
}

使用NetBeans部署JavaFX(有三种方式) 很简单,右键项目 -> 属性 -> Deployment
Yahoo Weather for Xian(JavaFX2.0) - 星期五 - 星期五
 
后台生成一个Jar文件,直接双击就可以运行了(和桌面程序一样)。

Yahoo Weather for Xian(JavaFX2.0) - 星期五 - 星期五
 

例子下载地址 http://download.csdn.net/detail/hurry70/3685918
开发完的感受。
1.使用JavaFX开发GUI,它本身就是Java程序,所以Java程序员入手快。
2.从后台取数据,到前台显示一气呵成,感觉很爽 (用同一种语言)。
3.希望有像Flex一样的所见即所得的IDE,这样就可以提高效率。
4.容易部署。
5.目前看来,如果你是Java程序员,又想学习富客户端技术 JavaFX是个不错选择,虽然它才刚起步。
  评论这张
 
阅读(1170)| 评论(5)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017