Java行者tsyj2007.blog.chinaunix.net
tsyj2007
全部博文(31)
2011年(2)
2010年(29)
ranson_z
qinglin9
liurhyme
ecloud
saintdra
zhujiang
ylke2007
ANHUI
大鬼不动
Bsolar
分类: Java
2010-09-11 18:58:48
package cn.edu.ytu.server; /** * 一个利用Java获取Google天气预报的例子 */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import org.w3c.dom.*; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.parsers.*; public class GoogleReport extends JFrame implements ActionListener { JPanel p1 = new JPanel(); JTextField tf = new JTextField(2); JButton but = new JButton("查询"); JPanel p2 = new JPanel(); JTextArea ta = new JTextArea(10, 40); public GoogleReport() { super("天气预报查询"); // 设置p1面板 p1.setLayout(new BorderLayout()); tf.addKeyListener(new TfListener());// 设置tf监听器 tf.setBorder(BorderFactory.createLineBorder(Color.blue, 1)); p1.add(tf, BorderLayout.CENTER); but.setBorder(BorderFactory.createLineBorder(Color.green));// 设置按钮边框颜色 // 增加but事件监听器 but.addActionListener(this); p1.add(but, BorderLayout.EAST); add(p1, BorderLayout.NORTH); // 设置p2面板 p2.setLayout(new GridLayout(1, 1)); JScrollPane sp = new JScrollPane(ta); ta.setEnabled(false); ta.setBorder(BorderFactory.createLineBorder(Color.red, 2)); p2.add(sp); add(p2, BorderLayout.CENTER); this.setSize(500, 400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new GoogleReport(); } /** * getWeather->由city值获得天气情况 * * @param city * @return 获得的天气情况的字符串 */ public String getWeather(String city) { try { URL ur = new URL("" + city); InputStream instr = ur.openStream(); String s; String str; BufferedReader in = new BufferedReader(new InputStreamReader(instr)); StringBuffer sb = new StringBuffer(); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("weather.txt"), "utf-8")); while ((s = in.readLine()) != null) { sb.append(s); } str = new String(sb); out.write(str); out.close(); in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } File f = new File("weather.txt"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); String str = null; try { DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = (NodeList) doc .getElementsByTagName("forecast_conditions");// 获得XML文档的forecast_conditions元素 NodeList n2 = nl.item(0).getChildNodes();// 获得第一个forecast_condition的全部子节点 str = "今天是" + n2.item(0).getAttributes().item(0).getNodeValue() + "," + n2.item(4).getAttributes().item(0).getNodeValue() + ","// 获得n2的第五个元素current_conditions + "最低气温" + n2.item(1).getAttributes().item(0).getNodeValue() + "℃,"// 获得n2的第二个元素low + "最高气温" + n2.item(2).getAttributes().item(0).getNodeValue() + "℃";// 获得n2的第二个元素hight } catch (Exception e) { } return str; } private class TfListener extends KeyAdapter { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { String s = tf.getText(); tf.setText(""); String ss = getWeather(s); // ta.setBorder(BorderFactory.createLineBorder(Color.RED,1)); ta.append(ss + "\n"); } } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == but) { String s = tf.getText(); tf.setText(""); String ss = getWeather(s); ta.append(ss + "\n"); } } }
上一篇:Java泛型简单总结
下一篇:50个常用的SQL语句
登录 注册