java天气查询
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class WeatherForecast extends JFrame{
static JButton btn_search=new JButton("查询");
static JTextField text=new JTextField(10);
static JLabel lab_city=new JLabel("城市: ");
static JLabel lab_today=new JLabel("今天 ");
static JLabel lab_tomorrow=new JLabel("明天 ");
static JLabel lab_afterTomorrow=new JLabel("后天 ");
static JLabel lab_Temperature=new JLabel("温度: ");
static JLabel lab_wind=new JLabel("风力: ");
static JLabel lab_Weather=new JLabel("天气: ");
double x,y;
Container con;
JPanel pan=new JPanel();
static WeatherForecast forecase=new WeatherForecast();
public void getFrame()throws Exception{
forecase.setVisible(true);
forecase.setSize(250,300);
forecase.setLocation(new Point(400,150));
forecase.setResizable(false);
//forecase.setDefaultCloseOperation(3);
forecase.add(pan);
pan.setLayout(null);
pan.add(lab_city);
pan.add(text);
pan.add(btn_search);
pan.add(lab_afterTomorrow);
pan.add(lab_Temperature);
pan.add(lab_tomorrow);
pan.add(lab_today);
pan.add(lab_Weather);
pan.add(lab_wind);
lab_city.setBounds(15,8,65,23);
text.setBounds(60,8,70,21);
btn_search.setBounds(150,6,70,23);
lab_today.setBounds(50,35,65,23);
lab_tomorrow.setBounds(110,35,65,23);
lab_afterTomorrow.setBounds(170,35,65,23);
lab_Weather.setBounds(15,55,65,23);
lab_Temperature.setBounds(15,75,65,23);
lab_wind.setBounds(15,95,65,23);
SwingUtilities.updateComponentTreeUI(btn_search);
SwingUtilities.updateComponentTreeUI(text);
text.grabFocus();
btn_search.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
final String city=text.getText();
pan.repaint();
new Thread(){
public void run(){
while(true){
try{
Thread.sleep(1000);
}catch(Exception e){}
drawTrend(main(city),pan);
}
}
}.start();
}
});
text.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(text.getText().trim().length()!=0){
btn_search.doClick();
}else{
JOptionPane.showMessageDialog(null,"请输入城市名称");
}
}
});
}
public void drawTrend(Weather w,JPanel j){
Graphics g=j.getGraphics();
g.setColor(Color.blue);
g.drawString(w.todayWeather, 53,69);
g.drawString(w.todayTemperautre, 53,89);
g.drawString(w.todayWind, 53,109);
g.drawString(w.tomorrowWeather, 110, 69);
g.drawString(w.tomorrowTemperature, 110, 89);
g.drawString(w.tomorrowWind, 110, 109);
g.drawString(w.afterTomorrowWeather, 170, 69);
g.drawString(w.afterTomorrowTemperature, 170, 89);
g.drawString(w.afterTomorrowWind, 170, 109);
g.setColor(new Color(200,200,209));
g.fillRect(25,120,196,130);
g.setColor(Color.red);
g.drawString("今天", 24,265);
g.drawString("明天", 104,265);
g.drawString("后天", 184,265);
g.setColor(Color.BLACK);
g.drawString("℃",5,120);
g.drawString(" 0", 5,185);
g.drawString(" 20", 5,152);
g.drawString(" 40", 5,120);
g.drawString("-20", 5,217);
g.drawString("-40", 5,250);
g.drawString("℃",227,120);
g.drawString("40",222,128);
g.drawString("20",222,158);
g.drawString("0 ",222,188);
g.drawString("-20",222,218);
g.drawString("-40",222,248);
drawWeatherTemperature(w,g);
}
public void drawWeatherTemperature(Weather wea,Graphics g){
try{
g.setColor(new Color(200,150,40));
if(wea.todayTemperautre.contains("~")){
Double firstTemperature=Double.parseDouble((wea.todayTemperautre.split("~")[0]));
Double secondTemperature=Double.parseDouble((wea.todayTemperautre.split("~")[1].split("℃")[0]));
double firstTem=firstTemperature.doubleValue();
double secondTem=secondTemperature.doubleValue();
double z1=0.00;
double z2=0.00;
int trendY=0;
int trendH=0;
int trendCH=0;
if(firstTemperature>0)
z1=firstTem*1.625+65.00;
else if(firstTemperature<0)
z1=65-1.625*firstTem;
else
z1=0.00;
if(secondTemperature>0)
z2=secondTem*1.625+65.00;
else if(secondTem<0)
z2=65-1.625*secondTem;
else
z2=0.00;
if(z1>z2){
trendY=(int)(130-z1+120+0.5);
trendH=(int)(z1+0.5);
trendCH=(int)(z1-z2+0.5);
}
else {
trendY=(int)(130-z2+120+0.5);
trendH=(int)(z2+0.5);
trendCH=(int)(z2-z1+0.5);
}
g.fillRect(30,trendY,30,trendH);
g.setColor(Color.blue);
g.fillRect(30,trendY,30,trendCH);
g.setColor(new Color(200,150,40));
System.out.println("firstTem:"+firstTem);
System.out.println("secondTem:"+secondTem);
}
if(!wea.todayTemperautre.contains("~")){
Double temperature=Double.parseDouble(wea.todayTemperautre.split("℃")[0]);
double Tem=temperature.doubleValue();
double h=0.00;
int trendY=0;
int trendH=0;
if(Tem>0)
h=Tem*1.625+65.00;
else if(Tem<0)
h=65-1.625*Tem;
else
h=0.00;
trendY=(int)(120+130-h+0.5);
trendH=(int)(h+0.5);
g.fillRect(30,trendY,30,trendH);
}
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
try{
if(wea.tomorrowTemperature.contains("~")){
Double firstTemperature=Double.parseDouble((wea.tomorrowTemperature.split("~")[0]));
Double secondTemperature=Double.parseDouble((wea.tomorrowTemperature.split("~")[1].split("℃")[0]));
double firstTem=firstTemperature.doubleValue();
double secondTem=secondTemperature.doubleValue();
double z1=0.00;
double z2=0.00;
int trendY=0;
int trendH=0;
int trendCH=0;
if(firstTemperature>0)
z1=firstTem*1.625+65.00;
else if(firstTemperature<0)
z1=65-1.625*firstTem;
else
z1=0.00;
if(secondTemperature>0)
z2=secondTem*1.625+65.00;
else if(secondTem<0)
z2=65-1.625*secondTem;
else
z2=0.00;
if(z1>z2){
trendY=(int)(130-z1+120+0.5);
trendH=(int)(z1+0.5);
trendCH=(int)(z1-z2+0.5);
}
else {
trendY=(int)(130-z2+120+0.5);
trendH=(int)(z2+0.5);
trendCH=(int)(z2-z1+0.5);
}
g.fillRect(90,trendY,30,trendH);
g.setColor(Color.blue);
g.fillRect(90,trendY,30,trendCH);
g.setColor(new Color(200,150,40));
}
if(!wea.tomorrowTemperature.contains("~")){
Double temperature=Double.parseDouble(wea.tomorrowTemperature.split("℃")[0]);
double Tem=temperature.doubleValue();
double h=0.00;
int trendY=0;
int trendH=0;
if(Tem>0)
h=Tem*1.625+65.00;
else if(Tem<0)
h=65-1.625*Tem;
else
h=0.00;
trendY=(int)(120+130-h+0.5);
trendH=(int)(h+0.5);
g.fillRect(90,trendY,30,trendH);
}
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
try{
if(wea.afterTomorrowTemperature.contains("~")){
Double firstTemperature=Double.parseDouble((wea.afterTomorrowTemperature.split("~")[0]));
Double secondTemperature=Double.parseDouble((wea.afterTomorrowTemperature.split("~")[1].split("℃")[0]));
double firstTem=firstTemperature.doubleValue();
double secondTem=secondTemperature.doubleValue();
double z1=0.00;
double z2=0.00;
int trendY=0;
int trendH=0;
int trendCH=0;
if(firstTemperature>0)
z1=firstTem*1.625+65.00;
else if(firstTemperature<0)
z1=65-1.625*firstTem;
else
z1=0.00;
if(secondTemperature>0)
z2=secondTem*1.625+65.00;
else if(secondTem<0)
z2=65-1.625*secondTem;
else
z2=0.00;
if(z1>z2){
trendY=(int)(130-z1+120+0.5);
trendH=(int)(z1+0.5);
trendCH=(int)(z1-z2+0.5);
}
else {
trendY=(int)(130-z2+120);
trendH=(int)z2;
trendCH=(int)(z2-z1);
}
g.fillRect(150,trendY,30,trendH);
g.setColor(Color.blue);
g.fillRect(150,trendY,30,trendCH);
g.setColor(new Color(200,150,40));
}
if(!wea.afterTomorrowTemperature.contains("~")){
Double temperature=Double.parseDouble(wea.afterTomorrowTemperature.split("℃")[0]);
double Tem=temperature.doubleValue();
double h=0.00;
int trendY=0;
int trendH=0;
if(Tem>0)
h=Tem*1.625+65.00;
else if(Tem<0)
h=65-1.625*Tem;
else
h=0.00;
trendY=(int)(120+130-h+0.5);
trendH=(int)(h+0.5);
g.fillRect(150,trendY,30,trendH);
}
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
public static Weather main(String str){
Scanner sc=new Scanner(System.in);
try{
URL url=new URL("http://www.baidu.com/s?wd="+URLEncoder.encode(str,"gbk")+"%CC%EC%C6%F8");
URLConnection con=url.openConnection();
BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
StringBuffer buf=new StringBuffer();
String s="";
while((s=reader.readLine())!=null){
buf.append(s);
}
String mes=buf.toString();
//今天天气
Weather wea=new Weather();
try{
String t="<strong class=\"altemp_special\">";
String m=mes.substring(mes.indexOf(t)+t.length());
String strong="</strong><br><strong>";
String span="</strong><br><span>";
String wd=m.substring(0,m.indexOf(strong));
String tq=m.substring(m.indexOf(strong)+strong.length());
tq=tq.substring(0,tq.indexOf(span));
String fl=m.substring(m.indexOf(span)+span.length());
fl=fl.substring(0,fl.indexOf("</span></td>"));
wea.todayTemperautre=wd;
wea.todayWeather=tq;
wea.todayWind=fl;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
//明天天气
try{
String WeatherStr="</div><strong class=\"altemp_special\">";
String []weatherMes=mes.split(WeatherStr);
String tomorrowWeather=weatherMes[2];
String tomorrowEnd="</span></td><td class=\"al_tr al_tl\">";
tomorrowWeather=tomorrowWeather.substring(0,tomorrowWeather.indexOf(tomorrowEnd));
String tomorrowTemperature=tomorrowWeather.substring(0,tomorrowWeather.indexOf("</strong>"));
String tomorrowDetail=tomorrowWeather.split("<strong>")[1].substring(0,tomorrowWeather.split("<strong>")[1].indexOf("</strong>"));
String tomorrowWind=tomorrowWeather.substring(tomorrowWeather.indexOf("<br><span>")+"<br><span>".length());
wea.tomorrowTemperature=tomorrowTemperature;
wea.tomorrowWeather=tomorrowDetail;
wea.tomorrowWind=tomorrowWind;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
//后天天气
try{
String afterTomorrowWeather=mes.split("/></a></div>")[3];
String afterTomorrowEnd="</span></td><td class=\"al_tl\">";
afterTomorrowWeather=afterTomorrowWeather.substring(0,afterTomorrowWeather.indexOf(afterTomorrowEnd));
String afterTomorrowTemperature=afterTomorrowWeather.substring(0,afterTomorrowWeather.indexOf("<br>"));
String aftertomorrowDetail=afterTomorrowWeather.split("<br>")[1];
String afterTomorrowWind=afterTomorrowWeather.substring(afterTomorrowWeather.indexOf("<br><span>")+"<br><span>".length());
wea.afterTomorrowTemperature=afterTomorrowTemperature;
wea.afterTomorrowWeather=aftertomorrowDetail;
wea.afterTomorrowWind=afterTomorrowWind;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
return wea;
}catch(StringIndexOutOfBoundsException ex){
JOptionPane.showMessageDialog(null,"输入的内容不合法");
ex.printStackTrace();
}catch(java.net.UnknownHostException s){
JOptionPane.showMessageDialog(null,"请检查你的网络连接");
}catch(java.net.NoRouteToHostException s){
JOptionPane.showMessageDialog(null,"请检查你的网络连接");
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
try {
String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
UIManager.setLookAndFeel(lookAndFeel);
new WeatherForecast().getFrame();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Weather{
String todayTemperautre;
String todayWeather;
String todayWind;
String tomorrowTemperature;
String tomorrowWeather;
String tomorrowWind;
String afterTomorrowTemperature;
String afterTomorrowWeather;
String afterTomorrowWind;
}
来源:CSDN
作者:wangyue23com
链接:https://blog.csdn.net/wangyue23com/article/details/103496921