Java Swing天气预报系统(数据库+天气后台管理)

Java Swing天气预报系统

今天写了个Java GUI的窗口应用,比较简单,主要用到数据库(mysql)、功能有:切换城市、展示不同城市天气、后台天气数据管理和展示

由于天气数据接口API为付费,免费的API接口返回的天气数据又比较少,所以这里我的天气数据都存在了数据库里,一共展示6天的天气

这里写目录标题

  • Java Swing天气预报系统
    • 应用窗口
    • 主要代码
    • 写在最后

应用窗口

1、天气主页面:
Java Swing天气预报系统(数据库+天气后台管理)_第1张图片
2、天气数据后台页面:
Java Swing天气预报系统(数据库+天气后台管理)_第2张图片
其中天气主页面支持切换城市、展示天气
天气后台管理页面支持天气数据增删改查

主要代码

天气主页面:WeatherMainFrame.java

package com.lisonglin.frame;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.lisonglin.dao.CityWeatherDaoImp;
import com.lisonglin.frame.WeatherMainFrame.CityItem;
import com.lisonglin.frame.WeatherMainFrame.time;
import com.lisonglin.model.CityWeather;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

public class WeatherMainFrame extends JFrame {
     

	JPanel jPanel_1 = new BackgroundPanel((new ImageIcon("img\\tianqi3.png")).getImage());
	JPanel jPanel_1_1 = new JPanel();
	JLabel jlabel_rc_time = new JLabel();
	JLabel jlabel_rl_time = new JLabel();

	JPanel jPanel_1_2 = new JPanel();
	JLabel jlabel_wendu_val = new JLabel();
	JLabel jlabel_windl_val = new JLabel();

	JPanel jPanel_1_3 = new JPanel();
	JLabel jlabel_shidu_txt = new JLabel();
	JLabel jlabel_windx_val = new JLabel();

	JPanel jPanel_1_4 = new JPanel();
	JLabel jlabel_city_txt = new JLabel();
	JLabel jlabel_update_time = new JLabel();

	JPanel jPanel_1_5 = new JPanel();
	JLabel jlabel_dq_time = new JLabel();
	JLabel jlabel_dq_week = new JLabel();

//	JPanel jPanel_2 = new JPanel(new FlowLayout(FlowLayout.LEADING, 40, 0));
	JPanel jPanel_2 = new BackgroundPanel((new ImageIcon("img\\tianqi2.png")).getImage());
//	JPanel jPanel_2 = new JPanel(new GridLayout(1, 5, 5, 5));
	JPanel jPanel_2_1 = new JPanel(new GridLayout(5, 1, 0, 0));
	JPanel jPanel_2_2 = new JPanel(new GridLayout(5, 1, 0, 0));
	JPanel jPanel_2_3 = new JPanel(new GridLayout(5, 1, 0, 0));
	JPanel jPanel_2_4 = new JPanel(new GridLayout(5, 1, 0, 0));
	JPanel jPanel_2_5 = new JPanel(new GridLayout(5, 1, 0, 0));
	JPanel jPanel_2_6 = new JPanel(new GridLayout(5, 1, 0, 0));

	JComboBox jComboBox = new JComboBox();

	JLabel imglabel1 = new JLabel("",JLabel.CENTER);
	JLabel imglabel2 = new JLabel("",JLabel.CENTER);
	JLabel imglabel3 = new JLabel("",JLabel.CENTER);
	JLabel imglabel4 = new JLabel("",JLabel.CENTER);
	JLabel imglabel5 = new JLabel("",JLabel.CENTER);
	JLabel imglabel6 = new JLabel("",JLabel.CENTER);

	// ImageIcon img1 = new ImageIcon();
	JLabel jlabel_week1 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_tianqi1 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fx1 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fl1 = new JLabel("",JLabel.CENTER);

	// ImageIcon img2 = new ImageIcon();
	JLabel jlabel_week2 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_tianqi2 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fx2 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fl2 = new JLabel("",JLabel.CENTER);

	// ImageIcon img3 = new ImageIcon();
	JLabel jlabel_week3 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_tianqi3 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fx3 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fl3 = new JLabel("",JLabel.CENTER);

	// ImageIcon img4 = new ImageIcon();
	JLabel jlabel_week4 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_tianqi4 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fx4 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fl4 = new JLabel("",JLabel.CENTER);

	// ImageIcon img5 = new ImageIcon();
	JLabel jlabel_week5 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_tianqi5 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fx5 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fl5 = new JLabel("",JLabel.CENTER);

	// ImageIcon img6 = new ImageIcon();
	JLabel jlabel_week6 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_tianqi6 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fx6 = new JLabel("",JLabel.CENTER);
	JLabel jlabel_fl6 = new JLabel("",JLabel.CENTER);

//	JPanel jPanel_3 = new JPanel(new FlowLayout(FlowLayout.LEADING, 40, 40));
	JPanel jPanel_3 = new BackgroundPanel((new ImageIcon("img\\tianqi4.png")).getImage());
	JPanel jPanel_3_1 = new JPanel(new GridLayout(6, 2, 15, 3));
	JLabel jlabel_ssd_txt = new JLabel();
	JLabel jlabel_cy_txt = new JLabel();
	JLabel jlabel_gm_txt = new JLabel();
	JLabel jlabel_zwx_txt = new JLabel();
	JLabel jlabel_yd_txt = new JLabel();
	JLabel jlabel_ys_txt = new JLabel();

	public WeatherMainFrame() throws IOException {
     

		this.setTitle("天气预报");
		this.setSize(2000, 100);
		this.setLayout(new BorderLayout(5,5)); // 为Frame窗口设置布局为BorderLayout
        
		jPanel_1.setOpaque(false);
		jPanel_1_1.setOpaque(false);
		jlabel_rc_time.setForeground(Color.WHITE);
		jlabel_rl_time.setForeground(Color.WHITE);
		jPanel_1_2.setOpaque(false);
		jPanel_1_3.setOpaque(false);
		jPanel_1_4.setOpaque(false);
		jPanel_1_5.setOpaque(false);
		jPanel_2.setLayout(new GridLayout(1, 5, 5, 5));
		jPanel_2_1.setOpaque(false);
		jPanel_2_2.setOpaque(false);
		jPanel_2_3.setOpaque(false);
		jPanel_2_4.setOpaque(false);
		jPanel_2_5.setOpaque(false);
		jPanel_1.setLayout(new FlowLayout(FlowLayout.LEADING, 40, 5));
		jPanel_3.setLayout(new FlowLayout(FlowLayout.LEADING, 40, 40));
		jPanel_3.setOpaque(false);
		jPanel_3_1.setOpaque(false);
		jlabel_ssd_txt.setForeground(Color.white);
		jlabel_cy_txt.setForeground(Color.white);
		jlabel_gm_txt.setForeground(Color.white);
		jlabel_zwx_txt.setForeground(Color.white);
		jlabel_yd_txt.setForeground(Color.white);
		jlabel_ys_txt.setForeground(Color.white);
//		jPanel_1.setBorder(BorderFactory.createLineBorder(Color.red, 2));
		jPanel_1.setBorder(BorderFactory.createEtchedBorder());
		
		// 表格布局
		jPanel_1_1.setLayout(new GridLayout(2, 3, 5, 10));
		JLabel jlabel_richu = new JLabel("日出:");
		jlabel_richu.setForeground(Color.white);
		JLabel jlabel_riluo = new JLabel("日落:");
		jlabel_riluo.setForeground(Color.white);
		jPanel_1_1.add(jlabel_richu);
		jPanel_1_1.add(jlabel_rc_time);
		jPanel_1_1.add(jlabel_riluo);
		jPanel_1_1.add(jlabel_rl_time);

		jPanel_1_2.setLayout(new GridLayout(2, 3, 5, 10));
		JLabel jlabel_wendu = new JLabel("温度:");
		JLabel jlabel_windl = new JLabel("风力:");
		jlabel_wendu.setForeground(Color.white);
		jlabel_windl.setForeground(Color.white);
		jlabel_wendu_val.setForeground(Color.white);
		jlabel_windl_val.setForeground(Color.white);
		jPanel_1_2.add(jlabel_wendu);
		jPanel_1_2.add(jlabel_wendu_val);
		jPanel_1_2.add(jlabel_windl);
		jPanel_1_2.add(jlabel_windl_val);

		jPanel_1_3.setLayout(new GridLayout(2, 3, 5, 10));
		JLabel jlabel_shidu = new JLabel("湿度:");
		JLabel jlabel_windx = new JLabel("风向:");
		jlabel_shidu.setForeground(Color.white);
		jlabel_windx.setForeground(Color.white);
		jlabel_shidu_txt.setForeground(Color.white);
		jlabel_windx_val.setForeground(Color.white);
		jPanel_1_3.add(jlabel_shidu);
		jPanel_1_3.add(jlabel_shidu_txt);
		jPanel_1_3.add(jlabel_windx);
		jPanel_1_3.add(jlabel_windx_val);

		jPanel_1_4.setLayout(new GridLayout(2, 3, 5, 10));
		JLabel jlabel_city = new JLabel("当前城市:");
		JLabel jlabel_update = new JLabel("更新时间:");
		jlabel_city.setForeground(Color.white);
		jlabel_update.setForeground(Color.white);
		jlabel_city_txt.setForeground(Color.white);
		jlabel_update_time.setForeground(Color.white);
		jPanel_1_4.add(jlabel_city);
		jPanel_1_4.add(jlabel_city_txt);
		jPanel_1_4.add(jlabel_update);
		jPanel_1_4.add(jlabel_update_time);

		jPanel_1_5.setLayout(new GridLayout(2, 3, 5, 2));
		JButton jbutton_ext_city = new JButton("切换城市");
		JButton jbutton_ext_tianqi = new JButton("天气后台");

		initCityList();
		jlabel_dq_time.setForeground(Color.white);
		jlabel_dq_week.setForeground(Color.white);
		jPanel_1_5.add(jlabel_dq_time);
		jPanel_1_5.add(jlabel_dq_week);
		Label label_xx = new Label("");
		label_xx.setVisible(false);
		jPanel_1_5.add(label_xx);
		jPanel_1_5.add(jComboBox);
		jPanel_1_5.add(jbutton_ext_city);
		jPanel_1_5.add(jbutton_ext_tianqi);
		

		jPanel_1.add(jPanel_1_1);
		jPanel_1.add(jPanel_1_2);
		jPanel_1.add(jPanel_1_3);
		jPanel_1.add(jPanel_1_4);
		jPanel_1.add(jPanel_1_5);

		jPanel_1.setPreferredSize(new Dimension(0, 68));

//		jPanel_2_1.setSize(250, 150);
        
		jlabel_week1.setForeground(Color.WHITE);
		jlabel_tianqi1.setForeground(Color.WHITE);
		jlabel_fx1.setForeground(Color.WHITE);
		jlabel_fl1.setForeground(Color.WHITE);
		jlabel_week2.setForeground(Color.WHITE);
		jlabel_tianqi2.setForeground(Color.WHITE);
		jlabel_fx2.setForeground(Color.WHITE);
		jlabel_fl2.setForeground(Color.WHITE);
		jlabel_week3.setForeground(Color.WHITE);
		jlabel_tianqi3.setForeground(Color.WHITE);
		jlabel_fx3.setForeground(Color.WHITE);
		jlabel_fl3.setForeground(Color.WHITE);
		jlabel_week4.setForeground(Color.WHITE);
		jlabel_tianqi4.setForeground(Color.WHITE);
		jlabel_fx4.setForeground(Color.WHITE);
		jlabel_fl4.setForeground(Color.WHITE);
		jlabel_week5.setForeground(Color.WHITE);
		jlabel_tianqi5.setForeground(Color.WHITE);
		jlabel_fx5.setForeground(Color.WHITE);
		jlabel_fl5.setForeground(Color.WHITE);
		
		jPanel_2_1.add(imglabel1);
		jPanel_2_1.add(jlabel_week1);
		jPanel_2_1.add(jlabel_tianqi1);
		jPanel_2_1.add(jlabel_fx1);
		jPanel_2_1.add(jlabel_fl1);
		jPanel_2_1.setBorder(BorderFactory.createLineBorder(Color.white, 2));

		jPanel_2_2.add(imglabel2);
		jPanel_2_2.add(jlabel_week2);
		jPanel_2_2.add(jlabel_tianqi2);
		jPanel_2_2.add(jlabel_fx2);
		jPanel_2_2.add(jlabel_fl2);
		jPanel_2_2.setBorder(BorderFactory.createLineBorder(Color.white, 2));

		jPanel_2_3.add(imglabel3);
		jPanel_2_3.add(jlabel_week3);
		jPanel_2_3.add(jlabel_tianqi3);
		jPanel_2_3.add(jlabel_fx3);
		jPanel_2_3.add(jlabel_fl3);
		jPanel_2_3.setBorder(BorderFactory.createLineBorder(Color.white, 2));

		jPanel_2_4.add(imglabel4);
		jPanel_2_4.add(jlabel_week4);
		jPanel_2_4.add(jlabel_tianqi4);
		jPanel_2_4.add(jlabel_fx4);
		jPanel_2_4.add(jlabel_fl4);
		jPanel_2_4.setBorder(BorderFactory.createLineBorder(Color.white, 2));

		jPanel_2_5.add(imglabel5);
		jPanel_2_5.add(jlabel_week5);
		jPanel_2_5.add(jlabel_tianqi5);
		jPanel_2_5.add(jlabel_fx5);
		jPanel_2_5.add(jlabel_fl5);
		jPanel_2_5.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));

		jPanel_2_6.add(imglabel6);
		jPanel_2_6.add(jlabel_week6);
		jPanel_2_6.add(jlabel_tianqi6);
		jPanel_2_6.add(jlabel_fx6);
		jPanel_2_6.add(jlabel_fl6);
		jPanel_2_6.setBorder(BorderFactory.createLineBorder(Color.white, 2));

		jPanel_2.add(jPanel_2_1);
		jPanel_2.add(jPanel_2_2);
		jPanel_2.add(jPanel_2_3);
		jPanel_2.add(jPanel_2_4);
		jPanel_2.add(jPanel_2_5);
//		jPanel_2.add(jPanel_2_6);
//		jPanel_2.setBorder(BorderFactory.createLineBorder(Color.red, 2));

		JLabel jlabel_ssd = new JLabel("舒适度");
		JLabel jlabel_cy = new JLabel("穿衣指数");
		JLabel jlabel_gm = new JLabel("感冒指数");
		JLabel jlabel_zwx = new JLabel("紫外线指数");
		JLabel jlabel_yd = new JLabel("运动指数");
		JLabel jlabel_ys = new JLabel("雨伞指数");
		jlabel_ssd.setForeground(Color.white);
		jlabel_cy.setForeground(Color.white);
		jlabel_gm.setForeground(Color.white);
		jlabel_zwx.setForeground(Color.white);
		jlabel_yd.setForeground(Color.white);
		jlabel_ys.setForeground(Color.white);
		jPanel_3_1.add(jlabel_ssd);
		jPanel_3_1.add(jlabel_ssd_txt);
		jPanel_3_1.add(jlabel_cy);
		jPanel_3_1.add(jlabel_cy_txt);
		jPanel_3_1.add(jlabel_gm);
		jPanel_3_1.add(jlabel_gm_txt);
		jPanel_3_1.add(jlabel_zwx);
		jPanel_3_1.add(jlabel_zwx_txt);
		jPanel_3_1.add(jlabel_yd);
		jPanel_3_1.add(jlabel_yd_txt);
		jPanel_3_1.add(jlabel_ys);
		jPanel_3_1.add(jlabel_ys_txt);
		jPanel_3.add(jPanel_3_1);

		jPanel_2.setPreferredSize(new Dimension(0, 100));
		jPanel_3.setPreferredSize(new Dimension(0, 200));

		CityItem currCI = (CityItem) jComboBox.getSelectedItem();
		String selectedCityCode = currCI.ccode;
		try {
     
			// 设置新城市的天气
			Weather set = new Weather(selectedCityCode);
			System.out.println(set.city);
			System.out.println(set.fchh);
			show(set);
			// tabbedPane.setSelectedIndex(0);
		} catch (IOException e1) {
     
			e1.printStackTrace();
		}

		jbutton_ext_city.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				// 获取当前选中的城市
				CityItem currCI = (CityItem) jComboBox.getSelectedItem();
				String selectedCityCode = currCI.ccode;
				try {
     
					// 设置新城市的天气
					Weather set = new Weather(selectedCityCode);
					System.out.println(set.city);
					System.out.println(set.fchh);
					show(set);
					// tabbedPane.setSelectedIndex(0);
				} catch (IOException e1) {
     
					e1.printStackTrace();
				}
			}
		});
		
		
		jbutton_ext_tianqi.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				// 天气后台操作
				System.out.println("进来了后台");
				WeatherAdminFrame wa = new WeatherAdminFrame();
				wa.setVisible(true);
			}
		});
		

		this.add(jPanel_1, BorderLayout.NORTH);
		// frame.add(button2,BorderLayout.WEST);
		this.add(jPanel_2, BorderLayout.CENTER);
		// frame.add(button4,BorderLayout.EAST);
		this.add(jPanel_3, BorderLayout.SOUTH);

		this.setBounds(300, 200, 900, 550);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public void show(Weather set) {
     

		jlabel_rc_time.setText("06:12");
		jlabel_rl_time.setText("18:20");

		jlabel_wendu_val.setText(set.temp1);
		jlabel_windl_val.setText(set.fl1);

		jlabel_shidu_txt.setText("88%");
		jlabel_windx_val.setText(set.wind1);

		jlabel_city_txt.setText(set.city);
		jlabel_update_time.setText("02:11");

		jlabel_dq_time.setText(set.date_y);
		jlabel_dq_week.setText(set.week);

		// 解析当前的年月日 并生成日历类
		StringBuilder builder1 = new StringBuilder();
		String time1 = set.date_y.substring(5, set.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 = set.date_y.substring(5 + time2.length() + 1, set.date_y.length() - 1);
		int y = Integer.parseInt(set.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);

		// 六天天气
		imglabel1.setIcon(new ImageIcon(set.img_title1));
		jlabel_week1.setText(day2.getDay() + "日" + day2.getweekOfDay());
//		jlabel_week1.CENTER;
		jlabel_tianqi1.setText(set.weather1);
		jlabel_fx1.setText(set.wind1);
		jlabel_fl1.setText(set.fl1);

		// 六天天气
		imglabel2.setIcon(new ImageIcon(set.img_title3));
		jlabel_week2.setText(day3.getDay() + "日" + day3.getweekOfDay());
		jlabel_tianqi2.setText(set.weather2);
		jlabel_fx2.setText(set.wind2);
		jlabel_fl2.setText(set.fl2);

		// 六天天气
		imglabel3.setIcon(new ImageIcon(set.img_title5));
		jlabel_week3.setText(day4.getDay() + "日" + day4.getweekOfDay());
		jlabel_tianqi3.setText(set.weather3);
		jlabel_fx3.setText(set.wind3);
		jlabel_fl3.setText(set.fl3);

		// 六天天气
		imglabel4.setIcon(new ImageIcon(set.img_title7));
		jlabel_week4.setText(day5.getDay() + "日" + day5.getweekOfDay());
		jlabel_tianqi4.setText(set.weather4);
		jlabel_fx4.setText(set.wind4);
		jlabel_fl4.setText(set.fl4);

		// 六天天气
		imglabel5.setIcon(new ImageIcon(set.img_title9));
		jlabel_week5.setText(day6.getDay() + "日" + day6.getweekOfDay());
		jlabel_tianqi5.setText(set.weather5);
		jlabel_fx5.setText(set.wind5);
		jlabel_fl5.setText(set.fl5);

//		// 六天天气
//		imglabel1.setIcon(new ImageIcon(set.img_title1));
//		jlabel_week1.setText(day2.getDay() + "日" + day2.getweekOfDay());
//		jlabel_tianqi1.setText(set.weather1);
//		jlabel_fx1.setText(set.wind1);
//		jlabel_fl1.setText(set.fl1);
		
		jlabel_ssd_txt.setText(set.index_co);
		jlabel_cy_txt.setText(set.index);
		jlabel_gm_txt.setText(set.index_cl);
		jlabel_zwx_txt.setText(set.index_uv);
		jlabel_yd_txt.setText(set.index_cl);
		jlabel_ys_txt.setText("11");

	}

	public static void main(String[] agrs) throws IOException {
     
		WeatherMainFrame demo = new WeatherMainFrame();
	}

	// 设置时间类(内部类)
	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);
			// }
			CityWeatherDaoImp cwp = new CityWeatherDaoImp();
			List<CityWeather> cinelist = cwp.queryAllWeather();
			CityItem[] cia = new CityItem[cinelist.size()];
			for (int i = 0; i < cinelist.size(); i++) {
     
				cia[i] = new CityItem(cinelist.get(i).getCity(), cinelist.get(i).getCode());
			}
			// cia[0] = new CityItem("北京", "101010100");
			// cia[1] = new CityItem("", "101010200");
			// cia[2] = new CityItem("北京", "101010100");
			// cia[3] = new CityItem("北京", "101010100");
			// for (int i = 0; i < 4; 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);
			// }
			jComboBox.setModel(new DefaultComboBoxModel(cia));
		} catch (Exception e) {
     
			e.printStackTrace();
		}
	}
	
	class BackgroundPanel extends JPanel
	{
     
		Image im;
		public BackgroundPanel(Image im)
		{
     
			this.im=im;
			this.setOpaque(true);
		}
		//Draw the back ground.
		public void paintComponent(Graphics g)
		{
     
			super.paintComponents(g);
			g.drawImage(im,0,0,this.getWidth(),this.getHeight(),this);
			
		}
	}

}

后台管理页面:WeatherAdminFrame.java

package com.lisonglin.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.lisonglin.model.Weather2;
import com.lisonglin.service.WeatherService;
import com.lisonglin.util.DateUtil;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class WeatherAdminFrame extends JFrame {
     

	private JPanel contentPane;
	private JTable table;
	private String[] columnCount= {
     "序号","天气","温度","时间","城市"};
	private List<Weather2> list;
	public static Weather2 wea;
	public static WeatherAdminFrame frame;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
     
		EventQueue.invokeLater(new Runnable() {
     
			public void run() {
     
				try {
     
					frame = new WeatherAdminFrame();
					//窗口居中
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
     
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public WeatherAdminFrame() {
     
		this.setTitle("天气后台管理");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 764, 469);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(29, 58, 692, 332);
		contentPane.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				quaryAll();
			}
		});
		button.setBounds(58, 22, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				new FromFjame().setVisible(true);
			}
		});
		button_1.setBounds(205, 22, 93, 23);
		contentPane.add(button_1);
		//全屏
//		setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		JButton button_2 = new JButton("修改");
		button_2.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				update();
				quaryAll();
			}
		});
		button_2.setBounds(357, 22, 93, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("删除");
		button_3.addActionListener(new ActionListener() {
     
			public void actionPerformed(ActionEvent e) {
     
				remove();
				quaryAll();
			}
		});
		button_3.setBounds(539, 22, 93, 23);
		contentPane.add(button_3);
		
	}
	//查询
	public void quaryAll() {
     
		WeatherService ss=new WeatherService();
		list = ss.queryAll();
		if(list==null) {
     
			JOptionPane.showMessageDialog(null, "服务器繁忙");
			return;
		}
		Object[][] data = DateUtil.listToArray(list);
		table.setModel(new DefaultTableModel(data, columnCount));
	}
	
	//删除
	private void remove() {
     
		int i = table.getSelectedRow();
		Weather2 s = list.get(i);
		int code = new WeatherService().delete(s.getId());
		if(code==0) {
     
			JOptionPane.showMessageDialog(null, "删除成功");
			return;
		}else {
     
			JOptionPane.showMessageDialog(null,DateUtil.errors.get(code) );
		}
		quaryAll();
	}
	
	//修改
	private void update() {
     
		int i = table.getSelectedRow();
		wea = list.get(i);
		new FromFjame().setVisible(true);
	}
}

天气数据封装页面:Weather.java

package com.lisonglin.frame;

import java.io.*;
import java.net.*;
import javax.swing.*;

import com.lisonglin.dao.CityWeatherDaoImp;
import com.lisonglin.model.CityWeather;

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");
//		URL url = new URL("http://www.weather.com.cn/data/cityinfo/" + Ctiyid + ".html");
		// URL url = new
		// URL("http://www.weather.com.cn/data/cityinfo/101010100.html" +
		// Ctiyid);
//		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");
		
		// 读取数据库的数据
		CityWeatherDaoImp cwp = new CityWeatherDaoImp();
		
		CityWeather cw = cwp.queryWeatherById(Cityid);
		
		System.out.println(cw.getCity());
		System.out.println(cw.getDate_y());
		System.out.println(cw.getImg_title1());

		// // 设置第一页吗的城市名称 日期
		// 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();

		// 设置第一页吗的城市名称 日期
		city = cw.getCity();
		week = cw.getWeek();
		date_y = cw.getDate_y();
		fchh = cw.getFchh();
		// 1到6天的天气
		weather1 = cw.getWeather1();
		weather2 = cw.getWeather2();
		weather3 = cw.getWeather3();
		weather4 = cw.getWeather4();
		weather5 = cw.getWeather5();
		weather6 = cw.getWeather6();
		// 1到6天的气温
		temp1 = cw.getTemp1();
		temp2 = cw.getTemp2();
		temp3 = cw.getTemp3();
		temp4 = cw.getTemp4();
		temp5 = cw.getTemp5();
		temp6 = cw.getTemp6();
		// 1到6天的风况
		wind1 = cw.getWind1();
		wind2 = cw.getWind2();
		wind3 = cw.getWind3();
		wind4 = cw.getWind4();
		wind5 = cw.getWind5();
		wind6 = cw.getWind6();
		// 1到6天的风速
		fl1 = cw.getFl1();
		fl2 = cw.getFl2();
		fl3 = cw.getFl3();
		fl4 = cw.getFl4();
		fl5 = cw.getFl5();
		fl6 = cw.getFl6();
		// 各种天气指数
		index = cw.getIndex();
		index_uv = cw.getIndex_uv();
		index_tr = cw.getIndex_tr();
		index_co = cw.getIndex_co();
		index_cl = cw.getIndex_cl();
		index_xc = cw.getIndex_xc();
		index_d = cw.getIndex_d();

		// 天气情况的图片
		img_title1 = "img/"+cw.getImg_title1();
		System.out.println(img_title1);
		img_title2 = "1";
		img_title3 = "img/"+cw.getImg_title3();
		img_title4 = "1";
		img_title5 = "img/"+cw.getImg_title5();
		img_title6 = "1";
		img_title7 = "img/"+cw.getImg_title7();
		img_title8 = "1";
		img_title9 = "img/"+cw.getImg_title9();
		img_title10 = "1";
		img_title11 = "img/"+cw.getImg_title11();
		img_title12 = "1";

		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";
		}

	}
}

主要项目结构采用MVC分层:
Java Swing天气预报系统(数据库+天气后台管理)_第3张图片

写在最后

图片可以进行自定义,同时数据也可以按照自己需求来修改
交流v:Code2Life2

你可能感兴趣的:(笔记,java,swing,gui,mysql)