虹猫云课堂:swing二级联动
一、Swing简介和javax.swing包
1、Swing是在AWT基础上发展而来的轻量级组件,与AWT相比不但改进了用户界面,而且所需的系统资源更少;
Swing是纯Java组件,使所有的应用程序在不同的平台上运行时具有本机外观和相同的行为;
javax.swing包包含了一系列Swing控件,如果要使用该包中的类,则必须显式地声明如下语句:import javax.swing.*。
2、javax.swing.JFrame
JFrame组件用于在Swing程序中创建窗体;
以下是JFrame常见的构造方法:
常用成员方法:
3、javax.swing.JPanel类
JPanel作为中间容器,用于将较小的轻量级组件组合在一起,默认情况下,它是透明的,与窗体的内容面板类似。以下是JPanel类常用的构造方法:
JPanel的常用方法
二、部分代码展示
1、实体层代码
2、配置层代码
package cn.com.config;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.util.Properties;
public abstract class LoadConfig {
private static Properties pop;
static {
pop = new Properties();
try {
pop.load(new FileReader(new File("config.Properties")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private LoadConfig() {
}
public static Properties getConfig() {
return pop;
}
}
3、数据层代码
package cn.com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import cn.com.config.LoadConfig;
public abstract class DBUtil {
private static Properties pop;
static {
pop = LoadConfig.getConfig();
}
private DBUtil() {
}
static {
try {
Class.forName(pop.getProperty("DRV"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn() throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection(pop.getProperty("URL"), pop.getProperty("USERNAME"), pop.getProperty("USERPWD"));
return conn;
}
public static Statement getStatement(Connection conn) throws SQLException {
return conn.createStatement();
}
public static PreparedStatement getPreparedStatement(Connection conn,String sql) throws SQLException {
return conn.prepareStatement(sql);
}
public static void free(ResultSet rs,Statement pstm,Connection conn) throws SQLException {
if(rs != null) {
rs.close();
}
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
}
public static void free(Statement pstm,Connection conn) throws SQLException {
if(pstm != null) {
pstm.close();
}
if(conn != null) {
conn.close();
}
}
public static void bindParam(PreparedStatement pstm,Object... params) throws SQLException {
for(int i = 1;i <= params.length;i++) {
pstm.setObject(i, params[i-1]);
}
}
}
4、数据操作层代码
package cn.com.daos.city;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.com.beans.CityInfoBean;
import cn.com.db.DBUtil;
public class CityInfoDAO {
public List<CityInfoBean> getCityInfo(int pid) {
List<CityInfoBean> list = new ArrayList<CityInfoBean>();
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
String sql = "select * from cityInfo where pid = ?";
CityInfoBean cib = null;
try {
conn = DBUtil.getConn();
pstm = DBUtil.getPreparedStatement(conn, sql);
DBUtil.bindParam(pstm, pid);
rs = pstm.executeQuery();
while(rs.next()) {
cib = new CityInfoBean();
cib.setCid(rs.getInt("cid"));
cib.setCname(rs.getString("cname"));
cib.setPid(rs.getInt("pid"));
list.add(cib);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DBUtil.free(rs, pstm, conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
}