【实例简介】
使用java实现的天气预报,有源代码和解释,源代码可以直接复制,运行之后是一个小程序
离国国国回图别国面图国B图程国图国国E国国E厘图国E国国图匡国目面国国田图国国国国
实用第一氵智慧密集
国国国国国H国国国国国
image=new Imagelcon(img/ico. png).getImage()
this. setlconImage(image)
∥
this setLayout(new Flow Layout(Flow Layout. LEFT, 10, 6)
ⅩML
DOM
jl. setPreferredSize(new Dimension(200, 201)
Document Builder Factory factory
this addeD)
DocumentBuilder Factory. newInstance(
jcb. setPreterredSize(new Dimension(150, 20)
Document Builder builder
this add(jcb)
factory. new Document Builder()
jb.setPreferredSize(new Dimension (60, 20)
this. addjb
Document doc
∥
builder parse(new File("CityList xml" )):
this init City List()
doc. normalize(
jb. addAction Listener(new ActionListener(
public void actionPerformed( ActionEvent e)
NodeList cityltems
doc. getElements By TagName!"city ltem")
∥
int city Count=cityltems getLength0
Cityltem currCI
Cityltemn cia=new CityltemIcity Count
(Cityltem)jcb. get Selectedltem(
for(int i-O; i
String selected City Code currCl ccode
Element city Element
(Element)cityltems item():
String ccode
YahooWeatherFramePre. this dispose()
city Element. getElements By TagName(" code")
}})
item(O).get First Child(). getNodeValue(
String cname
Dimension screen Size
city Element. get Elements By TagName('name")
Toolkit. getDefault Toolkit(). getScreen Size)
item(0).get First Child ().getNodeValue(
int center X=screen Size width/2: //
cia[]=new Cityltem(cname, ccode))
int center Y=screen Size. height/2: /
cb setModel
tW=250://
new DefaultComboBoxMode(cia))
nth=100;∥
Icatch(Exception ele. print StackTrace(); J
this setBounds(centerX-W/2, center Y-h/2-100, w, h)
init City list
CityList. xml
y
item
this setvisible(true)
Cityltem
Cityltem
this setDefaultCloseOperation(JFrame EXIT_ON_ CLOSE
Defaultcombo box Model
this setResizable(false; /
Yahoo WeatherFramepre
∥/ CityList. xml
lbic void init City List(0
Stylist
oublic static void main(String args[l
[new YahooWeather Pre(
Imaged
/i
ICo.pn
Ya
ahoo w eather rame
ng
class
package wy
import javax. swing. x
XML
CityList. xml init City list
Import Java. awt
public void init city List(
public class YahooWeatherFrame extends JFramef
Font font
50
2009.11
①C息脑编程技巧与维护》cJournalelectronicpublishinghouse.Allrightsreserved.htp/www.cnki.net
网络与通信
NETWORK coMMUnication
图国国国国国国国国国国国国图国国国国团团国黑图阴国图国团黑国国国园深国日国国国国国国国国国国国国国面国国国国国国国面国国国国国国图困国国团团国国图园国
new Font( GB2312,Font. PLAIN, 18)
∥1
this setDefaultCloseOperation(JFrame EXIT_ON_CLOSE
JPanel ipz=new JPanelo
this. setResizable(false
JPanel jps=new JPanel(
ublic void initWeather Frame(Image image)
JPanel ipx=new JPanel()
JsplitPane spy new JSplitPane(
Yahoo Weatherframe
JSplitPane. VERTICALSPLIT,jps, jpx);
init weatherframe
SPlit Pane jspz= new JSplitPane(
JSplitPane. HORIZONTAL_ SPLIT,jpz, jsp);
public void initWeather Frame(Image image
JLabel jl I odaylmg=new JLabel(); /
bel jl Today Title=new JLabel(: /
JLabel j Today Degree=new JLabelo://
this setTitle( yahoo
JLabel j Today chill=new JLabel(: /
this. setlconImage(image
jIToday Speed=new JLabelO): /
JLabel i Today Direction=new JLabel(: /
jpz. setBackground(new Color(231, 231, 231)
JLabel jI Today Humidity -new JLabel(,; /
jps setBackground(new Color(252, 237, 180)
abel j Today Pressure=new JLabelo): /
jpx set Background(new Color(176, 237, 255)
JLabel j Today visibility=new JLabelo);//
JLabel jl Today Sunrise=new JLabel(: /
this add(jspz, borderlayout CENTER)
JLabel j Today Sunset=new JLabelo): /
ispz. setDividerLocation (300
JLabel jlToday Publish Time=new JLabelo) /
spy. setDividerLocation(165)
∥
id
jspz. setDIvide
r Size(o)
JLabel toMorrow Time =new JLabelo: /
spy. setDivider Size(0)
abel j Tomorrowlmg=new JLabelo
jpz. setLayout(null
iI Todaylmg. setBounds(50, 5, 175, 120
JLabel jlTomorrowHigh=new JLabel(
ipz. add(j Todaylmg); j Today Title. setFont(font)
i Title set Bounds(5, 130, 260, 20)
JLabel jlTomorrowLow=new JLabelo
pz. add( Today Title);jI Today Degree. setFont(font)
jIToday Degree setBounds(5, 150, 260, 201:
JLabel jIDATTime=new JLabel0: /
jpz addl TodayDegree: j oday Chill setFont(font)
JLabel jIDATImg=new JLabel); /
jIToday chill setBounds(150, 150, 150, 20)
abel jIDATHigh=new JLabel); /
jpz. addjl Today Chill);jIToday Speed setFont(font)
JLabel jIDATLOW=new JLabel(: /
jIToday Speed setBounds(5, 170, 180, 20)
public YahooWeather Frame(String city Code
ipz. add jl Today Speed): jl Today Direction. setFont(font)
mage image)
i Today Direction. setBounds(190, 170, 170, 20)
oZ. add(j Today Direction
this initWeatherFrame(image
jITodayHumidity. setFont(font)
oday Humidity setBounds(5, 190, 170, 20
oz. add(jl Todayhumidity
Dimension screen Size
jIToday PressuresetFont(font)
Toolkit. getDefaultToolkit(get Screen Size(
jIToday Pressure setBounds(100, 190, 170, 20);
t center=screen Size width/2: /
jpz. addjl Today Pressure
t centerY-screen Size height/2: /
i Today Visibility. setFont(font)
ntw=620;/
IToday Visibility setBounds(5, 210, 170, 20)
nth=358;/
jpz. add(jl Today visibility
oday Sunrise. setFont(font)
this setBounds(centerX-w/2, centerY-h/2-100, w, h)
jITodaysunrise. set Bounds(5, 230, 150, 20)
thissetVisible(true)
pz. add(jlToday Sunrise
2009.11
o1994-2009 China academic journal electronic Publishing house. all rights reserved. ht
电脑绵程炫巧与维护57
离国国国回图别国面图国B图程国图国国E国国E厘图国E国国图匡国目面国国田图国国国国
实用第一氵智慧密集
国国某国国国国某国国
jIToday Sunset setFont(font)
Today Sunset. setBounds(5, 250, 150, 20)
public static void parseWeather
ipz. add(jl Today Sunset
(YahooWeather Frame ywf, String city Code)I
IToday Publish Time. setFont(font)
jIToday Publish Time set Bounds(5, 270, 290, 20)
public static String from CodeToText(int code)t
ipz. add(tODay Publish Time);
public static String
ips. setLayout(null
from DegreeToDirection Str(int degree)
jITomorrow Time setFont(font)
ilTomorrow Time setBounds(5, 5, 300, 20)
Yahoo Weather
ips. add(toMorrow Time)
ITomorrowImg. setBounds(55, 20, 175, 120
from Degree ToDirectionStr
ips. addjl I omorrowlmg)
ilTomorrowHigh. setFont(font)
ITomorrowHigh. setBounds(5, 140, 175, 20)
public static String
ips. addglTomorrowHigh)
from Degree ToDirection Str(int degree
tOMorrow LowsetFont(tont)
iITomorrowLow. set Bounds(155, 140, 175, 20)
if(degree>337&& degree<=360return
ips. add(jlTomorrowLow)
else if(degree>=0 & degree<=22)return".
else if(degree>22&& degree<=67return
ipx. setLayout(nul
lse if(degree>67 & degree<=1 12)return
iIDATTime. setFont(font)
else if(degree>1 12 & degree<=15/return
jIDATTime. setBounds(5, 5, 300, 20)
else if(degree>157&& degree<=202)return".]
px. addjIDATTime):
else if(degree>202 & degree<=247)return
jIDATImg. set Bounds(55, 20, 175, 120
else if(degree> 247 & degree<=292)return"
jpx. addjIDATImg): jIDATHigh set Font(font)
else it(degree>292 & degree<=337return
DATHigh. setBounds(5, 140, 175, 20)
else return
jpx. addgjIDATHigh)IDATLow setFont(font)
jIDATLOW. Set Bounds(155, 140, 175, 20)
from DegreeToDirection
px.addgjlDATLow)
tr
from Codetolext
Yahoo Weatherframe
public static String from Code ToText(int code)
Yahoo WeatherFrame Pre
switch( codel
case 0: return
case 1: return
new YahooWeatherFrame(selected City Code, image
case 2: return case 3: return
Yahoo Weatherframe Pre
case 4, return. case 5: return
case 6: return
case 7: return
case 8: return
case 9: return
case 10: return ""case 11: return "m
5
case 12: return
case 13: return
case 14: return
case 15: return
case 16: return. case 17: return
Yahoo Weather
case 18: return
case 19: return
package wyf;
case 20: return case 21: return
mport javax. xmL parsers
case 22: return
case 23: return
mport org. w 3c. dom
case 24: return case 25: return
import java. io. * import java. net. w
case 26: return
ort javax. swing. Import java. text
case 27: return
nport java util
case 28: return
public class YahooWeather
case 29: return
e 30: return
52
2009.11
①的息脑编程技巧与维护》cJournalelectronicpublishinghouse.Allrightsreserved.htp/www.cnki.net
网络与通信
NETWORK coMMUnication
图国国国国国国国国国国国国图国国国国团团国黑图阴国图国团黑国国国园深国日国国国国国国国国国国国国国面国国国国国国国面国国国国国国图困国国团团国国图园国
case 31: return
case 32: return
+nnm.getNamedltem(").getNodevalue()
case 33: return
case 34: return
/"
case 35: re
case 36: return
String direction Str=
case 37: return
case 38: return
nnm.getNamedltem(direction).getNode value(
case 39: return
case 40: return
It directionInt=Integer parseInt(direction Str)
case 41: return".case 42: return
ywf. j Today Direction. setText(
case 43: return
:case 44: return
+from ToDirection Str(directionInt)
case 45: return
case 46: return
case 47: return
links =doc. getElements By TagName
case 3200: return
weather: atmosphere);
default: return
nnm=links. item(0). getAttributes(
wf. j Today Humidity setText(
nnm.getNamedltem(humidity ).getNodeValue(+%)
XML
parse Weather
ywf. j Today Pressure setText(:
nnm. getNamedItem(pi
) getNode value0
wf. j Today Visibility. setText(
public static void parseWeather
((int)Double parse Double (nnm. getNamedltem(")
YahooWeatherFrame ywf, String city Code)
getNodeVa|ue0)*100)/10000.0+
try
∥
XML
DOM
links =doc. getElements By TagName( weather
DocumentBuilderFactory factory
astronomy")
DocumentBuilderFactory newInstance(
nnm=links. item(0).getAttributes(
Document Builder builder=
ywf. j Today Sunrise. setText
factory. new DocumentBuilder()
+nnm.getNamedltem ( sunrise").getNodevalue o
∥/URL
replace All(Is,));
Url url=new UrlcHttp: //xml. weatheR. yahoo. +
ywf. j Today Sunset. setText(
com/forecastrssu=c&p="+city Code
nnm. getNamedltem("sunset").getNode Value().replaceAll(
Document doc=builder parse(url. open Stream()
)
doc. normalize();∥
links =doc. getElements By TagName("weather: condition
NodeList links
nnm=links. item(O). getAttributes(
doc. getElements By TagName(pubDate")
nt code= Integer parseInt(
String date Str
nnm. getNamedltem(code"). getNodeValue()
links. item(0). getFirstchildo getNode Value()
ywf. j Today Title. setText(
Simple Date Format sdf=new Simple Date Format(
+from Code To Text(code)
EEE, dd MMM yyyy K: mm aa zzz, Locale. US);
Imagelcon i=new Imagelcon( img\+code+. png)
Date date=sdf parse(date Str)
f j Todaylmg. setlcon (i
Calendar cd=Calendar.getInstanceO
ywf. j Today Degree. setText(
cd set Time(date)
nnm.getNamedltem(temp").getNodeValue()+C")
date Str=(cd. get(Calendar YEAR))+
+(cd. get(Calendar MONTH)+1)+
links =doc. getElements By TagName(yweather: forecast")
+(cd. get(Calendar DATE)+
+cd. getiCalendar HOUR_OF_ DAY)+
nnm=links. item(0). getAttributes(
+((cd. get (Calendar. MINUTE) >9)?cd. get ( Calendar
code =Integer parseInt (nnm.getNamedltem ("code")
MINUTE)+“:“O"+Cd.ge( Calendar. minute)+"";
getNodevalue0
ywf. j IToday Publish T ime. setText(
+date Str
i=new Imagelcon"img\+code+.png)
ywf. j Tomorrowlmg. setlcon()
links =doc. getElements By TagName(weather: Wind);
sdf=new Simple Date Format(dd MMM yyYy", Locale. US)
NamedNodeMap nnm=links. item(0). getAttributes()
date=sdf parse
ywf jIToday Chill setText(
nnm. getNamedltem(cate). getNode Value()
+nnm.getNamedltem(chill ).getNode value()
cd= Calendar. getlns tance(
wf. j TOday Speed setText(
d set Time(date)
2009.11
o1994-2009 China academic journal electronic Publishing house. all rights reserved. ht
电脑绵程巧与维护53
离国国国回图别国面图国B图程国图国国E国国E厘图国E国国图匡国目面国国田图国国国国
实用第一氵智慧密集
国国某国国国国某国国
dateStr=(cd. get(Calendar YEAR))+
回x
立体)漏懂)查看2)收)工且m和m
+(cd. get( Calendar MONTH)+1)+
的:分分家件实田
+(cd. get(Calendar DATE))+
地址四同c:1 ats ard:8:i(k.桌面y1, atherisoftwarei
ywf. jITomorrow Time setText(
+date Str+
e
+from Code ToText(code))
联机订购归片
ywf. jITomorrowHigh set Text
nnm. getNamedltem(high).getNode Value(+C)
立件和文件夹任务
ywf. jITomorrowLow. setText(
9
nnm.getNamedltem(low").getNode Value0+C
将这个工件夹发布到
曾共字比件夹
32.pg
五它惶置
nnm=links. item(1).getAttributes(
code=-Integer. parseInt(
nnm.getNamedItem(code).getNode value()
号网上
=new Imagelcon("img\\+code+".png")
ywf. IDATImg. setlcon()
sdf=new Simple Date Format('dd MMM yYyy Locale US);
date=sdf parse (nnm. getNamedltem(date).getNode value
JavaSE 6.0
cd- Calendar. getInstance(
cd set Time(date)
dateStr=(d get(Calendar YEAR))+
+(cd. get(Calendar MONTH)+1)+
MANIFEST. MF
+(cd. get( Calendar DATE))+
ywf. jIDATTime.setText(
+date Str
Mainfest-Version: 1.0
++from Code ToText(code)
Main-Class: wyf. YahooWeatherFrame Pre
ywf. jIDATHigh. setText(
Created-By: 1.6.0(Sun Microsystems Inc.
nnm. getNamedltem( high").getNode Value()+C
ywf. jIDATLow.setText(
nm. getNamedltem("lw"). getNode∨alue0+"℃";
Icatch(Exception e)e print Stack Trace0: 1
MANIFEST. MF
class
JAXP
XML
path=C \Program Files Javajok1.6.0\bin
回x
中力:wy/ IahooweatherFrame pre1c1s读入
wyf/Rahul& herFrdme Prescil y[ lem.lass
Img
曾加: wyf/ ahooWeatherFrame Pre, class(读入=33)(写日=1938)
4
ng
class
jar cvfm YWC jar mainfest mf wyf/*
Yahoo Weather
JavaSe 6.0
Yahoo WeatherFrame
avaljdk.6.0'
YahooWeather parse Weather(this, city Code)
ar
Yah
Yahoo WeatherFrame
Yahoo WeatherFramePre o
ath-C: \Program Files wavajdk 16.0\bin
java -jar YWC jar
54
2009.11
心脑编裸技巧与维护
JournalElectronicPublishinghOuse.Allrightsreservedhttp://www.cnki.net
国国国国国回国国后国国国国国国是国国
实用第一氵智慧密集
国国国国国国国国国国国国国回国a国图图国国a国国图国国国国困面国国a国国国国图图ga函
storage CVCreateMem Storage(O)
radius CVRound ((r ->width r ->height
CVNamedWindow(
0.25米scae
CV WINDOW AUTOSIZE);∥
CvCirclel img. center, radius, color[O1, 3, 8, 0)
if(src) detect and draw(src); /
cVReleaselmage(&src)
CV Showlmage(
img
cvReleaseMem Storage( &storage
cVReleaselmage( &gray
CvReleaselmage( &small_img )
else
Afx Message BOX(L
EXE
EXE
cvReleaseHaar Classifier Cascade( &cascade
■人险检测
void CFace Detection View: detect_and_drawilpllmage *img
static Cv Scalar color[=0,0, 2551
double scale = 1.3
Ipllmage gray cVCreatelmage( cVSize (img->width,
mg->height),8, 1)
Ipllmage* small_img cv Createlmage( cvSize( cv Round
(img->width/scale), cvRound (img->height/scale), 8, 1)
nt
CvCvt Color( img, gray, CV_BGR2GRAY)
cvResize( gray, small_img, CV_INTER_LINEAR )
CVEcualizeHist( small_ing, small_img )
cvClearMem Storage( storage
∥
CVSeq* faces cvHaarDetectObjects small_img,
cascade, storage, 1.1, 2,0, cV Size(30, 30)):
for(i=0: i< (faces faces->total: 0): i++)
CVRect* r=(CvRect*)cVGet SeqElem( faces, i
CyPoint center
int radius
center x= cVRound ((r->x+r->width*0.5)*scale)
center. y CVRound((r->y r->height0.5)*scale
:2009-3-12)
54)
JavaSE
JAXP DOM
an
Yahoo
img
Javase
Yahoo Weather
YwC.jar
Web
Web
00928)
Yahoo
6
2009.11
电脑编技巧与维护
JournalElectronicPublishinghOuse.Allrightsreservedhttp://www.cnki.net
【实例截图】
【核心代码】