【实例简介】
使用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(: /
J