swing窗体天气预报

第一个类:Weather 天气实体类

package com.my.weatther;
import java.io.*;
import java.net.*;
import javax.swing.*;
import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public class Weather extends JFrame {

String Ctiyid;
URLConnection connectionData;
StringBuilder sb;
BufferedReader br;// 读取data数据流
JSONObject jsonData;
JSONObject weatherinfo;

//解析的参数
String city ;// 城市
String date_y;//日期
String week ;// 星期
String fchh ;// 发布时间

String weather1 ;// 1到6天天气情况
String weather2 ;
String weather3 ;
String weather4 ;
String weather5 ;
String weather6 ;

String wind1;//1到6天风况
String wind2;
String wind3;
String wind4;
String wind5;
String wind6;

String fl1;//风的等级
String fl2;
String fl3;
String fl4;
String fl5;
String fl6;


String temp1 ;// 1到6天的气温
String temp2 ;
String temp3 ;
String temp4 ;
String temp5 ;
String temp6 ;

String index;// 今天的穿衣指数
String index_uv ;// 紫外指数
String index_tr ;// 旅游指数
String index_co ;// 舒适指数
String index_cl ;// 晨练指数
String index_xc;//洗车指数
String index_d;//天气详细穿衣指数

String img_title1; // 1到6天的天气图片
String img_title2 ;//
String img_title3 ;//
String img_title4 ;//
String img_title5 ;//
String img_title6 ;//
String img_title7;
String img_title8 ;//
String img_title9 ;//
String img_title10 ;//
String img_title11 ;//
String img_title12;//

public Weather(String Cityid) throws IOException ,NullPointerException{
// 解析本机ip地址

this.Ctiyid = Cityid;
// 连接中央气象台的API
URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html");
connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
try {
br = new BufferedReader(new InputStreamReader(
connectionData.getInputStream(), "UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
} catch (SocketTimeoutException e) {
System.out.println("亲,网络没有连接");
} catch (FileNotFoundException e) {
System.out.println("亲,网络没有连接");
}


jsonData = JSONObject.fromObject(sb.toString());
System.out.println(jsonData);
weatherinfo = jsonData.getJSONObject("weatherinfo");

// 设置第一页吗的城市名称 日期
city = weatherinfo.getString("city").toString();
week = weatherinfo.getString("week").toString();
date_y = weatherinfo.getString("date_y").toString();
fchh = weatherinfo.getString("fchh").toString();
//1到6天的天气
weather1 = weatherinfo.getString("weather1").toString();
weather2 = weatherinfo.getString("weather2").toString();
weather3 = weatherinfo.getString("weather3").toString();
weather4 = weatherinfo.getString("weather4").toString();
weather5 = weatherinfo.getString("weather5").toString();
weather6 = weatherinfo.getString("weather6").toString();
//1到6天的气温
temp1 = weatherinfo.getString("temp1").toString();
temp2 = weatherinfo.getString("temp2").toString();
temp3 = weatherinfo.getString("temp3").toString();
temp4 = weatherinfo.getString("temp4").toString();
temp5 = weatherinfo.getString("temp5").toString();
temp6 = weatherinfo.getString("temp6").toString();
//1到6天的风况
wind1 = weatherinfo.getString("wind1").toString();
wind2 = weatherinfo.getString("wind2").toString();
wind3 = weatherinfo.getString("wind3").toString();
wind4 = weatherinfo.getString("wind4").toString();
wind5 = weatherinfo.getString("wind5").toString();
wind6 = weatherinfo.getString("wind6").toString();
//1到6天的风速
fl1 = weatherinfo.getString("fl1").toString();
fl2 = weatherinfo.getString("fl2").toString();
fl3 = weatherinfo.getString("fl3").toString();
fl4 = weatherinfo.getString("fl4").toString();
fl5 = weatherinfo.getString("fl5").toString();
fl6 = weatherinfo.getString("fl6").toString();
//各种天气指数
index = weatherinfo.getString("index").toString();
index_uv = weatherinfo.getString("index_uv").toString();
index_tr = weatherinfo.getString("index_tr").toString();
index_co= weatherinfo.getString("index_co").toString();
index_cl = weatherinfo.getString("index_cl").toString();
index_xc = weatherinfo.getString("index_xc").toString();
index_d = weatherinfo.getString("index_d").toString();
//天气情况的图片
img_title1 = weatherinfo.getString("img_title1").toString();
img_title2 = weatherinfo.getString("img_title2").toString();
img_title3 = weatherinfo.getString("img_title3").toString();
img_title4 = weatherinfo.getString("img_title4").toString();
img_title5 = weatherinfo.getString("img_title5").toString();
img_title6 = weatherinfo.getString("img_title6").toString();
img_title7 = weatherinfo.getString("img_title7").toString();
img_title8 = weatherinfo.getString("img_title8").toString();
img_title9 = weatherinfo.getString("img_title9").toString();
img_title10 = weatherinfo.getString("img_title10").toString();
img_title11 = weatherinfo.getString("img_title11").toString();
img_title12 = weatherinfo.getString("img_title12").toString();

System.out.println(city);

}

//设定天气情况的图片
public String setimage (String w) {
if (w.equals("晴")) {
return "image/0.png";
}else if (w.equals("多云")) {
return "image/1.png";
}else if (w.equals("阴")) {
return "image/2.png";
}else if (w.equals("小雨")) {
return "image/3.png";
}else if (w.equals("中雨")) {
return "image/4.png";
}else if (w.equals("大雨")) {
return "image/5.png";
}
else if (w.equals("暴雨")) {
return "image/6.png";
}else if (w.equals("雷阵雨")) {
return "image/7.png";
}else if (w.equals("阵雨")) {
return "image/8.png";
}else if (w.equals("雨夹雪")) {
return "image/9.png";
}else if (w.equals("小雪")) {
return "image/10.png";
}
else if (w.equals("中雪")) {
return "image/11.png";
}else if (w.equals("大雪")) {
return "image/12.png";
}else if (w.equals("暴雪")) {
return "image/13.png";
}else if (w.equals("小到中雨")) {
return "image/15.png";
}
else if (w.equals("中到大雨")) {
return "image/16.png";
}else if (w.equals("大到暴雨")) {
return "image/17.png";
}else if (w.equals("雾")) {
return "image/18.png";
}else if (w.equals("小到中雨")) {
return "image/15.png";
}else {
return "image/6.png";
}

}
}

第二个类:WeatherFrame 窗体
package com.my.weatther;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

@SuppressWarnings("serial")
public class WeatherFrame extends JFrame {

Font font1 = new Font("楷体_GB2312", Font.BOLD, 13);// 字体
Font font2 = new Font("楷体_GB2312", Font.BOLD, 13);
Font font3 = new Font("楷体_GB2312", Font.PLAIN, 12);
// 设置任务栏图标
PopupMenu popupMenu = new PopupMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();

String time;// 日历时间
URLConnection connectionIP;// IP
String Cityid;// 城市的代码

Image image;// 软件图标
JTabbedPane tabbedPane; // 分4页显示
JPanel Tomorrowpanel;
JPanel Morepanel;
JPanel Aboutpanel;
JPanel Sharepanel;
JSplitPane Todaypanel;// 分开今日天气面板为2部分
JPanel left = new JPanel();// 今天天气左边面板
JPanel right = new JPanel();// 今日天气右边面板

// 关于天气面板的内容
JLabel chooseCity;// 设置城市
JComboBox jcb;
JButton chooseButton;
JLabel share;//分享天气
JButton shareButton;
JTextArea area;
String message=null;

static JLabel source = new JLabel("天气来源:国家气象局卫星探测中心");
static JLabel author = new JLabel("作者:Mengyao");
static JLabel version = new JLabel("版本:城市天气1.0beta版");
// 显示在面板中的内容
Weather wea;// weather类
JLabel city = new JLabel();// 城市
JLabel fchh = new JLabel();//发布时间
JLabel city1 = new JLabel();
JLabel week = new JLabel();// 星期
JLabel week1 = new JLabel();
JLabel date_y = new JLabel();// 今日时间
JLabel date_y1 = new JLabel();
JLabel Image1 = new JLabel();// today右边的图标

JLabel weather1 = new JLabel();// 天气情况
JLabel weather2 = new JLabel();
JLabel weather3 = new JLabel();
JLabel weather4 = new JLabel();
JLabel weather5 = new JLabel();
JLabel weather6 = new JLabel();

JLabel wind1 = new JLabel();// 风况
JLabel wind2 = new JLabel();
JLabel wind3 = new JLabel();
JLabel wind4 = new JLabel();
JLabel wind5 = new JLabel();
JLabel wind6 = new JLabel();

JLabel fl1 = new JLabel();// 风速
JLabel fl2 = new JLabel();
JLabel fl3 = new JLabel();
JLabel fl4 = new JLabel();
JLabel fl5 = new JLabel();
JLabel fl6 = new JLabel();

JLabel temp1 = new JLabel();// 气温
JLabel temp2 = new JLabel();
JLabel temp3 = new JLabel();
JLabel temp4 = new JLabel();
JLabel temp5 = new JLabel();
JLabel temp6 = new JLabel();

JLabel index = new JLabel();// 穿衣指数
JLabel index_uv = new JLabel();// 紫外线
JLabel index_tr = new JLabel();// 旅游
JLabel index_co = new JLabel();// 舒适指数
JLabel index_cl = new JLabel();// 运动指数
JLabel index_xc = new JLabel();// 洗车指数

public static void main(String[] args) throws IOException {
@SuppressWarnings("unused")
WeatherFrame wf = new WeatherFrame();
}

public WeatherFrame() throws IOException {

this.setTitle("城市天气");
image = new ImageIcon("img/ico.png").getImage();
this.setIconImage(image);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.width / 2;// 屏幕中央x 坐标
int centerY = screenSize.height / 2; // 屏幕中央y 坐标
int w = 465;// 本窗体宽度
int h = 365;// 本窗体高度
// 设置窗体出现在屏幕中央
this.setBounds(centerX - w / 2, centerY - h / 2, w, h);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

tabbedPane = new JTabbedPane();
ImageIcon icon1 = new ImageIcon("img/today.png");
ImageIcon icon2 = new ImageIcon("img/tomorrow.png");
ImageIcon icon3 = new ImageIcon("img/more.png");
ImageIcon icon5 = new ImageIcon("img/about.png");

Todaypanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
Todaypanel.setDividerLocation(225);
Todaypanel.setDividerSize(0);
//left.setBorder(BorderFactory.createLoweredSoftBevelBorder());
//right.setBorder(BorderFactory.createLoweredSoftBevelBorder());
tabbedPane.addTab(" 今天天气 ", icon1, Todaypanel, "亲,这是今天的天气");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

Tomorrowpanel = new JPanel();
//Tomorrowpanel.setBorder(BorderFactory.createLoweredSoftBevelBorder());
tabbedPane.addTab(" 明天天气 ", icon2, Tomorrowpanel, "亲,这是明天的天气");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

Morepanel = new JPanel();
// Morepanel.setBorder(BorderFactory.createLoweredSoftBevelBorder());
tabbedPane.addTab(" 未来天气 ", icon3, Morepanel, "亲,这是未来的天气");
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

Aboutpanel = new JPanel();
//Aboutpanel.setBorder(BorderFactory.createLoweredSoftBevelBorder());
tabbedPane.addTab(" 关于天气 ", icon5, Aboutpanel, "关于天气");
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);

Aboutpanel.setLayout(null);
chooseCity = new JLabel("设置城市:");
jcb = new JComboBox();
jcb.setEditable(false);//不能输入
chooseButton = new JButton("确定");
shareButton = new JButton("分享");
share = new JLabel("你将复制以下天气内容到剪切板");
area = new JTextArea();
area.setEditable(false);//不能输入

chooseCity.setBounds(80,30,70,30);
jcb.setBounds(155,30,110,30);
chooseButton.setBounds(270, 30, 70, 30);
share.setBounds(80,70,190,30);
shareButton.setBounds(270,70,70,30);
area.setBounds(80,110,260,100);
area.setFont(font3);
author.setBounds(80,230,100,20);
author.setFont(font3);
version.setBounds(80,250,200,20);
version.setFont(font3);

Aboutpanel.add(chooseCity);
Aboutpanel.add(jcb);
Aboutpanel.add(chooseButton);
Aboutpanel.add(share);
Aboutpanel.add(shareButton);
Aboutpanel.add(area);
Aboutpanel.add(author);
Aboutpanel.add(version);

chooseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取当前选中的城市
CityItem currCI = (CityItem) jcb.getSelectedItem();
String selectedCityCode = currCI.ccode;
try {
// 设置新城市的天气
Weather set = new Weather(selectedCityCode);
show(set);
tabbedPane.setSelectedIndex(0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

shareButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clipboard clipboard=getToolkit().getSystemClipboard();
StringSelection text =new StringSelection(message);
clipboard.setContents(text,null);
}
});

this.initCityList();
this.add(tabbedPane);
show(new Weather(this.IP()));

}


public void show(Weather weather) {

left.setLayout(null);

// 解析当前的年月日 并生成日历类
StringBuilder builder1 = new StringBuilder();
String time1 = weather.date_y.substring(5, weather.date_y.length() - 1);
char a[] = time1.toCharArray();
for (int i = 0; i < a.length - 1; i++) {
if (a[i] != '月')
builder1.append(a[i]);
else
break;
}
String time2 = builder1.toString();
String time3 = weather.date_y.substring(5 + time2.length() + 1,
weather.date_y.length() - 1);
int y = Integer.parseInt(weather.date_y.substring(0, 4));
int m = Integer.parseInt(time2);
int d = Integer.parseInt(time3);
Calendar c = new GregorianCalendar(y, m - 1, d);

// 未来五天的时间
c.add(Calendar.DAY_OF_MONTH, +1);
time day2 = new time(c);
c.add(Calendar.DAY_OF_MONTH, +1);
time day3 = new time(c);
c.add(Calendar.DAY_OF_MONTH, +1);
time day4 = new time(c);
c.add(Calendar.DAY_OF_MONTH, +1);
time day5 = new time(c);
c.add(Calendar.DAY_OF_MONTH, +1);
time day6 = new time(c);

// 设置今日天气的内容
weather1.setBounds(40, 5, 200, 80);
weather1.setFont(font1);
weather1.setText(weather.weather1);
weather1.setIcon(new ImageIcon(weather.setimage(weather.img_title1)));
left.add(weather1);

city.setFont(font1);
city.setBounds(15, 85, 235, 20);
city.setText(weather.city);
left.add(city);

week.setFont(font1);
week.setBounds(15, 105, 235, 20);
week.setText(weather.week);
left.add(week);

date_y.setFont(font1);
date_y.setBounds(15, 125, 235, 20);
date_y.setText(weather.date_y);
left.add(date_y);

JLabel seperate = new JLabel(
"------------------------------------------------");
seperate.setBounds(15, 145, 235, 20);
left.add(seperate);

temp1.setFont(font1);
temp1.setBounds(15, 165, 235, 20);
temp1.setText("气温:" + weather.temp1);
left.add(temp1);

wind1.setFont(font1);
wind1.setBounds(15, 185, 235, 20);
wind1.setText("风向:" + weather.wind1);
left.add(wind1);

fl1.setFont(font1);
fl1.setBounds(15, 205, 235, 20);
fl1.setText("风速:" + weather.fl1);
left.add(fl1);

// 今日面板右边的天气设置
right.setLayout(null);
Image1.setBounds(35, 5, 235, 60);
Image1.setFont(font2);
Image1.setText("更多天气信息");
Image1.setIcon(new ImageIcon("img/index.png"));
right.add(Image1);

index.setBounds(15, 65, 235, 35);
index.setFont(font2);
index.setText("穿衣指数:" + weather.index);
index.setIcon(new ImageIcon("img/cy.png"));
right.add(index);

index_uv.setBounds(15, 100, 235, 35);
index_uv.setFont(font2);
index_uv.setText("紫外指数:" + weather.index_uv);
index_uv.setIcon(new ImageIcon("img/uv.png"));
right.add(index_uv);

index_tr.setBounds(15, 135, 235, 35);
index_tr.setFont(font2);
index_tr.setText("旅游指数:" + weather.index_tr);
index_tr.setIcon(new ImageIcon("img/tr.png"));
right.add(index_tr);

index_co.setBounds(15, 170, 235, 35);
index_co.setFont(font2);
index_co.setText("舒适指数:" + weather.index_co);
index_co.setIcon(new ImageIcon("img/co.png"));
right.add(index_co);

index_cl.setBounds(15, 205, 235, 35);
index_cl.setFont(font2);
index_cl.setText("晨练指数:" + weather.index_cl);
index_cl.setIcon(new ImageIcon("img/cl.png"));
right.add(index_cl);

index_xc.setBounds(15, 240, 235, 35);
index_xc.setFont(font2);
index_xc.setText("洗车指数:" + weather.index_xc);
index_xc.setIcon(new ImageIcon("img/xc.png"));
right.add(index_xc);

// 设置明日天气的内容
Tomorrowpanel.setLayout(null);
Tomorrowpanel.add(weather2);
weather2.setBounds(100, 20, 200, 70);
weather2.setText(weather.weather2);
weather2.setIcon(new ImageIcon(weather.setimage(weather.img_title3)));

city1.setBounds(100, 90, 200, 30);
city1.setText(weather.city);
Tomorrowpanel.add(city1);

week1.setBounds(100, 120, 200, 30);
week1.setText(day2.getweekOfDay());
Tomorrowpanel.add(week1);

date_y1.setBounds(100, 150, 200, 30);
date_y1.setText(day2.getMonth() + "月" + day2.getDay() + "日");
Tomorrowpanel.add(date_y1);

temp2.setBounds(100, 180, 360, 30);
temp2.setText("气温:" + weather.temp2);
Tomorrowpanel.add(temp2);

source.setBounds(100,210,200,30);
Tomorrowpanel.add(source);

fchh.setText("发布时间:"+weather.date_y+weather.fchh+"时");
fchh.setBounds(100,240,200,30);
Tomorrowpanel.add(fchh);

// 设置未来天气的内容
Morepanel.setLayout(null);
// 未来几天的时间显示
JLabel d3 = new JLabel(day3.getMonth() + "月" + day3.getDay() + "日");
JLabel d4 = new JLabel(day4.getMonth() + "月" + day4.getDay() + "日");
JLabel d5 = new JLabel(day5.getMonth() + "月" + day5.getDay() + "日");
JLabel d6 = new JLabel(day6.getMonth() + "月" + day6.getDay() + "日");
JLabel w3 = new JLabel(day3.getweekOfDay());
JLabel w4 = new JLabel(day4.getweekOfDay());
JLabel w5 = new JLabel(day5.getweekOfDay());
JLabel w6 = new JLabel(day6.getweekOfDay());

d3.setBounds(20, 10, 70, 72);
w3.setBounds(100, 10, 50, 72);
weather3.setBounds(170, 10, 170, 72);
weather3.setText(weather.weather3);
weather3.setIcon(new ImageIcon(weather.setimage(weather.img_title5)));
temp3.setBounds(350, 10, 70, 72);
temp3.setText(weather.temp3);
Morepanel.add(d3);
Morepanel.add(w3);
Morepanel.add(weather3);
Morepanel.add(temp3);

d4.setBounds(20, 82, 60, 72);
w4.setBounds(100, 82, 50, 72);
weather4.setBounds(170, 82, 170, 72);
weather4.setText(weather.weather4);
weather4.setIcon(new ImageIcon(weather.setimage(weather.img_title7)));
temp4.setBounds(350, 82, 70, 72);
temp4.setText(weather.temp4);
Morepanel.add(d4);
Morepanel.add(w4);
Morepanel.add(weather4);
Morepanel.add(temp4);

d5.setBounds(20, 154, 70, 72);
w5.setBounds(100, 154, 50, 72);
weather5.setBounds(170, 154, 170, 72);
weather5.setText(weather.weather5);
weather5.setIcon(new ImageIcon(weather.setimage(weather.img_title9)));
temp5.setBounds(350, 154, 70, 72);
temp5.setText(weather.temp5);
Morepanel.add(d5);
Morepanel.add(w5);
Morepanel.add(weather5);
Morepanel.add(temp5);

d6.setBounds(20, 226, 70, 72);
w6.setBounds(100, 226, 50, 72);
weather6.setBounds(170, 226, 170, 72);
weather6.setText(weather.weather6);
weather6.setIcon(new ImageIcon(weather.setimage(weather.img_title11)));
temp6.setBounds(350, 226, 70, 72);
temp6.setText(weather.temp6);
Morepanel.add(d6);
Morepanel.add(w6);
Morepanel.add(weather6);
Morepanel.add(temp6);

message = weather.city+",今日"+weather.weather1+",气温:"+weather.temp1+"。温馨提示:"
+weather.index_d+"【yane为您播报】";
area.setLineWrap(true);
area.setFont(font3);
//area.setBorder(BorderFactory.createLoweredSoftBevelBorder());
area.setText(message);


Todaypanel.repaint();
Tomorrowpanel.repaint();
Morepanel.repaint();
Aboutpanel.repaint();

}

// 解析IP
public String IP() throws UnsupportedEncodingException, IOException {
URL ip;
ip = new URL("http://172.18.1.12:81/g/");
try {
connectionIP = ip.openConnection();
connectionIP.setConnectTimeout(500);
BufferedReader readip = new BufferedReader(new InputStreamReader(
connectionIP.getInputStream(), "UTF-8"));
String IPline = null;
StringBuilder IPString = new StringBuilder();
while ((IPline = readip.readLine()) != null) {
IPString.append(IPline);
}
Cityid = IPString.toString().substring(29, 38);
return Cityid;
} catch (SocketTimeoutException e) {
Cityid = "101010100";
return Cityid;
}
}

// 设置时间类(内部类)
class time {

int year;
int month;
int weekDay;
int day;
String weekOfDay;
Calendar c;

// 解析当前的年月日 并生成日历
public time(Calendar c) {

// 解析当前的年月日 并生成日历
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
day = c.get(Calendar.DAY_OF_MONTH);
weekDay = c.get(Calendar.DAY_OF_WEEK);

switch (weekDay) {
case 1:
weekOfDay = "星期天";
break;
case 2:
weekOfDay = "星期一";
break;
case 3:
weekOfDay = "星期二";
break;
case 4:
weekOfDay = "星期三";
break;
case 5:
weekOfDay = "星期四";
break;
case 6:
weekOfDay = "星期五";
break;
case 7:
weekOfDay = "星期六";
default:
break;
}

}

public int getYear() {
return year;
}

public int getMonth() {
return month;
}

public int getDay() {
return day;
}

public String getweekOfDay() {
return weekOfDay;
}

}

// city的名称和代码类
class CityItem {
String cname;// 城市名称
String ccode;// 城市代码

// CityItem 内部类构造器
public CityItem(String cname, String ccode) {
this.cname = cname;
this.ccode = ccode;
}

// 重写toString 方法
@Override
public String toString() {
return cname;
}
}

// 解析存储城市的city的文件CityList.xml
public void initCityList() {
try {
// 为解析XML 文件创建DOM 对象
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// 解析XML 文件
Document doc = builder.parse(new File("CityList.xml"));
// 规格化
doc.normalize();
// 解析城市列表
NodeList cityItems = doc.getElementsByTagName("cityItem");
int cityCount = cityItems.getLength();
CityItem[] cia = new CityItem[cityCount];
for (int i = 0; i < cityCount; i++) {
Element cityElement = (Element) cityItems.item(i);
String ccode = cityElement.getElementsByTagName("code").item(0)
.getFirstChild().getNodeValue();
String cname = cityElement.getElementsByTagName("name").item(0)
.getFirstChild().getNodeValue();
cia[i] = new CityItem(cname, ccode);
}
jcb.setModel(new DefaultComboBoxModel(cia));
} catch (Exception e) {
e.printStackTrace();
}
}

}

以上代码复制可用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值