- 浏览: 17722 次
- 性别:
- 来自: 北京
文章分类
最新评论
第一个类: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();
}
}
}
以上代码复制可用
发表评论
-
JAVA生成缩略图
2014-07-15 17:52 878package com.my.image; import j ... -
JDK代理模式
2014-07-15 17:08 553第一个类:jobService package com.my. ... -
java反射强制赋值
2014-07-15 16:59 701package com.my.reflect; import ... -
读取properties
2014-07-07 18:15 431package com.my.properties; imp ... -
properties小案例
2014-07-16 17:29 401package com.my.properties; imp ... -
Timer小案例
2014-07-04 17:24 617package com.my.timer; import j ... -
io文件复制读写以及打gz和zip压缩包,递归删除文件以及文件夹
2014-07-04 17:15 826package com.my.io; import java ... -
javaMail邮件发送
2014-07-04 17:13 425第一个邮件基本信息类:MailSenderInfo pack ... -
ftp上传下载
2014-07-04 16:57 686package com.my.ftp; import jav ...
相关推荐
基于java swing的天气预报查询软件(含数据库文件).zip 计算机专业,软件工程专业,通信工程大学生课程设计 自己大三的时候写的 适合大家做课程设计,写毕业设计也可以参考 基于Java的课程设计
这个“基于java swing的天气预报查询软件”是一个利用Java Swing库开发的应用程序,旨在帮助用户查询不同地区的天气预报信息。下面将详细介绍Java Swing的相关知识点以及可能在这款软件中涉及的技术。 1. **Java ...
在本项目"一个简单Swing窗体"中,开发者使用Swing来创建了一个基本的交互式窗口,具备点击事件的统计功能。让我们深入探讨Swing的相关知识点。 首先,Swing提供了许多组件,如按钮(JButton)、文本框(JTextField...
### Java_Swing窗体种类简介 #### Swing概述 Swing是Java中的一种图形用户界面工具包,用于构建桌面应用程序。Swing提供了丰富的组件库,这些组件具有高度的可定制性,使得开发者能够创建出功能强大且外观美观的...
Swing窗体的经典步骤,1.基本规范 1)定义一个Start入口类,只有入口方法main(),里面写最开始的代码; 2)一个窗体一个类,所有的窗体类名都以Frm结尾; 3)显示窗体: //实体化窗体 LoginFrm loginFrm = new ...
总的来说,通过使用nimrodlf-1.0e库,开发者不仅可以为Swing窗体增添各种美观的皮肤,还可以自由调整窗体的透明度,使得Java GUI应用更具吸引力。不过要注意,透明度调整可能需要额外处理鼠标和键盘事件的捕获,因为...
基于Java+Swing实现天气预报系统(高分课程设计)已获导师指导并通过的95分的高分期末大作业项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 该系统功能完善、界面美观、操作简单、功能...
基础组件示例 登录、注册、修改密码、用户管理、个人信息管理、数据管理万能模板 使用java集合存储数据,不用数据库 教学视频 https://www.bilibili.com/video/BV1mz421d7nG/
【作品名称】:基于java swing的天气预报查询软件 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 软件配置 Java环境...
Java Swing 窗体布局是Java图形用户界面(GUI)设计的重要组成部分,它决定了组件在窗口中的排列方式和外观。Swing提供了多种布局管理器,每种都有其独特的特性和用途,下面我们将详细讨论这些布局管理器。 1. **...
Swing天气预报是一款基于Java Swing库开发的桌面应用程序,它能够展示实时的天气信息。Swing是Java标准版(Java SE)的一部分,提供了一套丰富的组件和工具,用于创建具有图形用户界面(GUI)的应用程序。这个应用...
这份"swing窗体控件学习文档"很可能是对Swing控件的详细讲解,帮助开发者掌握如何在Java程序中使用这些控件。 Swing提供了许多基本和高级的组件,用于创建窗口、菜单、按钮、文本输入框、滚动面板等元素。下面我们...
在本项目“Jave Swing 窗体图书管理系统”中,开发者利用Swing组件创建了一个完整的图书管理应用程序,适用于毕业设计或者作为实践项目。这个系统可能包含了用户登录、图书信息管理等多个功能模块。 1. **Swing组件...
系统主要通过输入城市名字(可通过电脑IP查询所属城市,查询所属城市IP接口:http://ip.360.cn/IPShare/info),再通过天气接口:https://geoapi.heweather.net/v2/city/lookup查询出城市天气状态。 运行环境:idea...
Java Swing 窗体图标修改 在 Java Swing 中,修改窗体图标可以使用两种方法:使用 Toolkit 类和使用 ImageIcon 类。 方法一:使用 Toolkit 类 使用 Toolkit 类可以非常方便地修改窗体图标。首先,我们需要获取...
本例子中的"Java使用swing 组件窗体美化的例子.rar"是一个很好的学习资源,它展示了如何利用Swing来设计美观的窗口应用,并且包含窗体风格的动态切换功能。 Swing是Java AWT(Abstract Window Toolkit)的扩展,...
本教程将深入讲解如何使用Java Swing来实现桌面右下角的弹窗效果,以及如何让Swing窗体闪烁,以吸引用户的注意力。 首先,Java Swing是Java AWT(Abstract Window Toolkit)的扩展,提供了更现代、更美观的GUI组件...
### Swing窗体开发中的菜单和工具栏知识点详解 #### 一、引言 在Java Swing框架中,菜单和工具栏是构建用户界面不可或缺的一部分。它们不仅提供了丰富的功能,还极大地提高了开发者的效率。本文档旨在详细介绍...
一个非常漂亮的java swing窗体程序
基于swing实现窗体拖拽和拉伸 swing是一个Java标准库,提供了一些基本的图形用户界面(GUI)组件,如按钮、文本框、标签等。swing可以用来创建图形用户界面,但它也可以用于创建复杂的图形用户界面,如拖拽和拉伸...