您的位置:首页 > 编程语言 > Java开发

学生信息管理系统 -- javase

2015-05-21 00:00 423 查看
摘要: 很久之前写的一个东西,昨天帮同学安装java环境的时候才发现,留下来当作记念吧

包结构

├─dao 数据库操作接口
│ │ StudentDao.java 对student表的操作
│ │
│ └─impl 数据库操作实现
│ StudentDaoImpl.java

├─db 数据库连接工具
│ db-config.properties
│ laolangDB.java

├─domain 此包下的类对应数据库中的表
│ Student.java

└─ui 界面
ManagerMainWindow.java 主界面
StudentInsertDialog.java 插入对话框
StudentTableModel.java 表格模型
StudentUpdateDialog.java 更新对话框

数据库和表

mysql> select database();
+---------------------+
| database()          |
+---------------------+
| studentmanager1.1.1 |
+---------------------+
1 row in set

mysql> show tables;
+-------------------------------+
| Tables_in_studentmanager1.1.1 |
+-------------------------------+
| student                       |
+-------------------------------+
1 row in set

mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stuId   | int(11)     | NO   | PRI | NULL    |       |
| stuName | varchar(20) | NO   |     | NULL    |       |
| stuAge  | int(11)     | NO   |     | NULL    |       |
| stuSex  | varchar(5)  | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set

mysql> select * from student;
+-------+---------+--------+--------+
| stuId | stuName | stuAge | stuSex |
+-------+---------+--------+--------+
|  1001 | 小代码  |     25 | 男     |
|  1002 | 小龙女  |     28 | 女     |
|  1003 | 老狼    |     35 | 男     |
+-------+---------+--------+--------+
3 rows in set

mysql>


具体代码实现:

com.laolang.dao

com.laolang.dao.StudentDao

package com.laolang.dao;

import java.sql.SQLException;
import java.util.List;

import com.laolang.domain.Student;

/**
* 数据库操作接口
*/
public interface StudentDao {

/**
* 插入学生基本信息.
*
* @param stu
*            学生对象
* @throws SQLException
*             the SQL exception
*/
public void insertStudent(Student stu) throws SQLException;

/**
* 删除学生基本信息.
*
* @param stuId
*            学生编号
* @throws SQLException
*             the SQL exception
*/
public void deleteStudent(int stuId) throws SQLException;

/**
* 更新学生基本信息.
*
* @param stu
*            学生对象
* @throws SQLException
*             the SQL exception
*/
public void updateStudent(Student stu) throws SQLException;

/**
* S通过编号查询学生基本信息.
*
* @param stuId
*            学生编号
* @return the student
* @throws SQLException
*             the SQL exception
*/
public Student selectStudentById(int stuId) throws SQLException;

/**
* 查询所有学生基本信息.
*
* @return the list
* @throws SQLException
*             the SQL exception
*/
public List<Student> selectStudentAll() throws SQLException;
}


com.laolang.dao.impl

com.laolang.dao.impl.StudentDaoImpl

package com.laolang.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.laolang.dao.StudentDao;
import com.laolang.db.laolangDB;
import com.laolang.domain.Student;

/**
* 数据库操作实现
*/
public class StudentDaoImpl implements StudentDao {

/** dbutils 工具类对象 */
private QueryRunner runner;

/**
* Instantiates a new student dao impl.
*/
public StudentDaoImpl() {
runner = new QueryRunner();
}

/*
* 插入学生信息
*
* @see com.laolang.dao.StudentDao#insertStudent(com.laolang.domain.Student)
*/
@Override
public void insertStudent(Student stu) throws SQLException {
String insertStudent = "insert into student( stuId, stuName, stuAge, stuSex ) values(?,?,?,?)";
runner.update(laolangDB.getConnection(), insertStudent, stu.getStuId(),
stu.getStuName(), stu.getStuAge(), stu.getStuSex());
}

/*
* 删除学生信息
*
* @see com.laolang.dao.StudentDao#deleteStudent(int)
*/
@Override
public void deleteStudent(int stuId) throws SQLException {
String deleteStudentById = "delete from student where stuId = ?";
runner.update(laolangDB.getConnection(), deleteStudentById, stuId);
}

/*
* 更新学生基本信息
*
* @see com.laolang.dao.StudentDao#updateStudent(com.laolang.domain.Student)
*/
/*
* (non-Javadoc)
*
* @see com.laolang.dao.StudentDao#updateStudent(com.laolang.domain.Student)
*/
@Override
public void updateStudent(Student stu) throws SQLException {
String updateStudent = "update student set stuName = ?, stuAge = ?, stuSex = ? where stuId = ?";
runner.update(laolangDB.getConnection(), updateStudent,
stu.getStuName(), stu.getStuAge(), stu.getStuSex(),
stu.getStuId());
}

/*
* 通过编号查询学生基本信息
*
* @see com.laolang.dao.StudentDao#selectStudentById(int)
*/

@Override
public Student selectStudentById(int stuId) throws SQLException {
Student stu = null;
String selectStudentById = "select stuName, stuAge, stuSex from student where stuId = ?";
stu = runner.query(laolangDB.getConnection(), selectStudentById,
new BeanHandler<Student>(Student.class), stuId);
stu.setStuId(stuId);

return stu;
}

/*
* 查询所有学生基本信息
*
* @see com.laolang.dao.StudentDao#selectStudentAll()
*/

@Override
public List<Student> selectStudentAll() throws SQLException {
String selectStudentAll = "select stuId, stuName, stuAge, stuSex from student";
List<Student> studentList = runner.query(laolangDB.getConnection(),
selectStudentAll, new BeanListHandler<Student>(Student.class));

return studentList;
}

}


com.laolang.db

com.laolang.db.laolangDB

package com.laolang.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

/**
* 数据库连接和关闭工具类
*/
public class laolangDB {

/** 数据库连接地址 */
private static String URL;

/** 数据库用户名 */
private static String USERNAME;

/** 数据库密码 */
private static String USERPASSWORD;

/** mysql 驱动 */
private static String DRIVER;

/** The rb. */
private static ResourceBundle rb = ResourceBundle
.getBundle("com.laolang.db.db-config");

/**
* 使用静态代码块加载驱动
*/
static {
URL = rb.getString("jdbc.url");
USERNAME = rb.getString("jdbc.username");
USERPASSWORD = rb.getString("jdbc.userpassword");
DRIVER = rb.getString("jdbc.driver");

try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* 获得链接.
*
* @return the connection
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, USERPASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}

return conn;
}

/**
* 关闭链接.
*
* @param rs            the rs
* @param ps            the ps
* @param conn            the conn
*/
public static void closeConnection(ResultSet rs, Statement ps,
Connection conn) {
try {
if (null != rs)
rs.close();
if (null != ps)
ps.close();
if (null != conn)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


com.laolang.db.db-config.properties

jdbc.url=jdbc:mysql://localhost:3306/studentmanager1.1.1
jdbc.username=root
jdbc.userpassword=root
jdbc.driver=com.mysql.jdbc.Driver


com.laolang.domain

com.laolang.domain.Student

package com.laolang.domain;

/**
* 学生对象,对应数据库中 student 表
*/
public class Student {

/**
* Instantiates a new student.
*/
public Student() {
super();
}

/**
* Instantiates a new student.
*
* @param stuId
*            the stu id
* @param stuName
*            the stu name
* @param stuAge
*            the stu age
* @param stuSex
*            the stu sex
*/
public Student(int stuId, String stuName, int stuAge, String stuSex) {
super();
this.stuId = stuId;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuSex = stuSex;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge="
+ stuAge + ", stuSex=" + stuSex + "]";
}

/**
* Gets the stu id.
*
* @return the stu id
*/
public int getStuId() {
return stuId;
}

/**
* Sets the stu id.
*
* @param stuId
*            the new stu id
*/
public void setStuId(int stuId) {
this.stuId = stuId;
}

/**
* Gets the stu name.
*
* @return the stu name
*/
public String getStuName() {
return stuName;
}

/**
* Sets the stu name.
*
* @param stuName
*            the new stu name
*/
public void setStuName(String stuName) {
this.stuName = stuName;
}

/**
* Gets the stu age.
*
* @return the stu age
*/
public int getStuAge() {
return stuAge;
}

/**
* Sets the stu age.
*
* @param stuAge
*            the new stu age
*/
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}

/**
* Gets the stu sex.
*
* @return the stu sex
*/
public String getStuSex() {
return stuSex;
}

/**
* Sets the stu sex.
*
* @param stuSex
*            the new stu sex
*/
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}

/** 学生编号 */
private int stuId;

/** 学生姓名 */
private String stuName;

/** 学生年龄 */
private int stuAge;

/** 学生性别 */
private String stuSex;
}


com.laolang.ui

com.laolang.ui.ManagerMainWindow

package com.laolang.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.laolang.domain.Student;

/**
* 功能:学生信息管理系统主界面 版本:studentManager1.1.1 作者:小代码
*
*/
public class ManagerMainWindow extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;

/**
* The main method.
*
* @param args
*            the arguments
*/
public static void main(String[] args) {
@SuppressWarnings("unused")
ManagerMainWindow mmw = new ManagerMainWindow();
}

/**
* Instantiates a new manager main window.
*/
public ManagerMainWindow() {
init();
this.setActionCommand();

this.setTitle("学生信息管理系统");
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

/**
* 初始化窗体
*/
private void init() {
// 创建组件
this.lbId = new JLabel("待查学生编号");
this.butInsert = new JButton("增加");
this.butDelete = new JButton("删除");
this.butUpdate = new JButton("修改");
this.butSelect = new JButton("查询");
this.butShowAll = new JButton("显示所有");
this.tfId = new JTextField(10);
this.jpNorth = new JPanel();
this.jpSouth = new JPanel();
this.stm = new StudentTableModel();
stm.showAllData();
this.jt = new JTable(stm);
this.jsp = new JScrollPane(jt);

// 将组件添加到面板
jpNorth.add(lbId);
jpNorth.add(tfId);
jpNorth.add(butSelect);

jpSouth.add(butInsert);
jpSouth.add(butDelete);
jpSouth.add(butUpdate);
jpSouth.add(butShowAll);

// 将面板添加到窗体
this.add(jpNorth, BorderLayout.NORTH);
this.add(jsp, BorderLayout.CENTER);
this.add(jpSouth, BorderLayout.SOUTH);
}

/**
* Sets the action command.
*/
private void setActionCommand() {
this.butInsert.addActionListener(this);
this.butDelete.addActionListener(this);
this.butUpdate.addActionListener(this);
this.butSelect.addActionListener(this);
this.butShowAll.addActionListener(this);

this.butInsert.setActionCommand("insert");
this.butDelete.setActionCommand("delete");
this.butUpdate.setActionCommand("update");
this.butSelect.setActionCommand("select");
this.butShowAll.setActionCommand("show");
}

/*
* (non-Javadoc) 事件处理
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
// 如果用户点击添加
if (e.getActionCommand().equals("insert")) {
// System.out.println("insert");
// 弹出添加对话框,并得到添加对话框中输入的值
Student stu = new StudentInsertDialog(this, "添加学生信息", true)
.getStu();
// 如果没有点取消
if (null != stu) {

StudentTableModel stutm = new StudentTableModel();// 生成新的model

stutm.insertStu(stu);// 执行插入

jt.setModel(stutm);// 更新表格model
}

}
// 如果点击删除
else if (e.getActionCommand().equals("delete")) {
// System.out.println("delete");
// 得到用户选择的行的行号
int delIndex = jt.getSelectedRow();
// 如果没有选择任何行,则提醒选择一行
if (-1 == delIndex) {
JOptionPane.showMessageDialog(this, "请选择一行之后,再进行删除操作", "警告",
JOptionPane.WARNING_MESSAGE);
}
if (-1 != delIndex) {

String idStr = (String) stm.getValueAt(delIndex, 0);
int id = Integer.valueOf(idStr).intValue();
// System.out.println(id);

StudentTableModel stuTm = new StudentTableModel();// 生成亲的model
stuTm.deleteStu(id);// 执行删除

jt.setModel(stuTm);// 更新表格model
}

}
// 如果用户选择更新
else if (e.getActionCommand().equals("update")) {
// System.out.println("update");

// 得到用户选择的行的行号
int updataRowIndex = this.jt.getSelectedRow();
// 如果没有选择任何行,则提醒选择一行
if (-1 == updataRowIndex) {
JOptionPane.showMessageDialog(this, "请选择一行之后,再进行修改操作", "警告",
JOptionPane.WARNING_MESSAGE);
}
if (-1 != updataRowIndex) {
Student stu = new Student();

stu.setStuId(Integer.valueOf(
(String) stm.getValueAt(updataRowIndex, 0)).intValue());
stu.setStuName((String) stm.getValueAt(updataRowIndex, 1));
stu.setStuAge(Integer.valueOf(
(String) stm.getValueAt(updataRowIndex, 2)).intValue());
stu.setStuSex((String) stm.getValueAt(updataRowIndex, 3));

stu = new StudentUpdateDialog(this, "修改学生信息", true, stu)
.getStu();
// 如果修改了学生信息,则更新
if (null != null) {

StudentTableModel stutm = new StudentTableModel();// 万籁新的model
stutm.updateStu(stu);// 执行更新

jt.setModel(stutm);// 更新表格model

// System.out.println(stu.toString());
}

}

}
// 如果选择查询
else if (e.getActionCommand().equals("select")) {
// System.out.println("select");
// 得到输入的学生编号
int id = Integer.valueOf(tfId.getText()).intValue();

StudentTableModel stutm = new StudentTableModel();// 生成新 model
stutm.selectStuId(id);// 执行查询

jt.setModel(stutm);// 更新表格model

}
// 如果选择显示所有
else if (e.getActionCommand().equals("show")) {
// System.out.println("show");
StudentTableModel stutm = new StudentTableModel();// 生成新的model
stutm.showAllData();// 执行显示所有
jt.setModel(stutm);// 更新表格model
}
}

/** 输入编号标签 */
private JLabel lbId;

/** 接收编号的输入框 */
private JTextField tfId;

/** 插入按钮 */
private JButton butInsert;

/** 删除按钮 */
private JButton butDelete;

/** 更新按钮 */
private JButton butUpdate;

/** 查询按钮 */
private JButton butSelect;

/** 显示所有按钮 */
private JButton butShowAll;

/** 滚动窗格 */
private JScrollPane jsp;

/** 表格 */
private JTable jt;

/** 用于初始化表格的model */
private StudentTableModel stm;

/** 北部paenl */
private JPanel jpNorth;

/** 南部paenl */
private JPanel jpSouth;
}


com.laolang.ui.StudentInsertDialog

package com.laolang.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.laolang.domain.Student;

/**
* 添加学生信息对话框
*/
public class StudentInsertDialog extends JDialog implements ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Instantiates a new student insert dialog.
*/
public StudentInsertDialog() {
super();
}

/**
* Instantiates a new student insert dialog.
*
* @param owner
*            the owner
* @param title
*            the title
* @param modal
*            the modal
*/
public StudentInsertDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
init();
setComm();
this.setSize(300, 180);
this.setVisible(true);
this.setResizable(false);

}

/**
* 初始化
*/
private void init() {
// 创建组件
this.labelId = new JLabel("编号");
this.labelName = new JLabel("姓名");
this.labelAge = new JLabel("年龄");
this.labelSex = new JLabel("性别");

this.tfId = new JTextField(20);
this.tfName = new JTextField(20);
this.tfAge = new JTextField(20);
this.tfSex = new JTextField(20);

this.butOk = new JButton("确定");
this.butCanel = new JButton("取消");

this.jpCenterLeft = new JPanel();
this.jpCenterRight = new JPanel();
this.jpCenter = new JPanel();
this.jpSouth = new JPanel();

// 设置布局
this.setLayout(new BorderLayout());
this.jpCenterLeft.setLayout(new GridLayout(6, 1));
this.jpCenterRight.setLayout(new GridLayout(6, 1));
this.jpCenter.setLayout(new FlowLayout());
this.jpSouth.setLayout(new FlowLayout());

// 添加组件到面板
this.jpCenterLeft.add(this.labelId);
this.jpCenterLeft.add(this.labelName);
this.jpCenterLeft.add(this.labelAge);
this.jpCenterLeft.add(this.labelSex);

this.jpCenterRight.add(this.tfId);
this.jpCenterRight.add(this.tfName);
this.jpCenterRight.add(this.tfAge);
this.jpCenterRight.add(this.tfSex);

this.jpCenter.add(this.jpCenterLeft);
this.jpCenter.add(this.jpCenterRight);

this.jpSouth.add(this.butOk);
this.jpSouth.add(this.butCanel);

// 面板添加到窗体
this.add(this.jpCenter);
this.add(this.jpSouth, BorderLayout.SOUTH);

}

/**
* 注册监听、设置命令.
*/
private void setComm() {
this.butOk.addActionListener(this);
this.butCanel.addActionListener(this);

this.butOk.setActionCommand("ok");
this.butCanel.setActionCommand("canel");
}

/*
* (non-Javadoc) 事件处理
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("ok")) {
// 如果点击 确定,则创建一个Student对象,并对各个属性赋值
this.stu = new Student();
this.stu.setStuId(Integer.valueOf(this.tfId.getText()).intValue());
this.stu.setStuName(this.tfName.getText());
this.stu.setStuAge(Integer.valueOf(this.tfAge.getText()).intValue());
this.stu.setStuSex(this.tfSex.getText());
// 隐藏对话框
this.setVisible(false);

} else if (e.getActionCommand().equals("canel")) {
// 如果点击取消,则置Student为空
this.stu = null;
// 隐藏对话框
this.setVisible(false);
}
}

/** The label id. */
private JLabel labelId;

/** The label name. */
private JLabel labelName;

/** The label age. */
private JLabel labelAge;

/** The label sex. */
private JLabel labelSex;

/** The tf id. */
private JTextField tfId;

/** The tf name. */
private JTextField tfName;

/** The tf age. */
private JTextField tfAge;

/** The tf sex. */
private JTextField tfSex;

/** The but ok. */
private JButton butOk;

/** The but canel. */
private JButton butCanel;

/** The jp center left. */
private JPanel jpCenterLeft;

/** The jp center right. */
private JPanel jpCenterRight;

/** The jp center. */
private JPanel jpCenter;

/** The jp south. */
private JPanel jpSouth;

/** The stu. */
private Student stu;

/**
* Gets the stu.
*
* @return the stu
*/
public Student getStu() {
return stu;
}

}


com.laolang.ui.StudentTableModel

package com.laolang.ui;

import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.laolang.dao.StudentDao;
import com.laolang.dao.impl.StudentDaoImpl;
import com.laolang.domain.Student;

/**
* 主窗口中,表格模型
*/
public class StudentTableModel extends AbstractTableModel{

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Instantiates a new student table model.
*/
public StudentTableModel(){
init();
}

/**
* 初始化,只完成表头部分
*/
private void init(){
rowData = new Vector();
colunmNames = new Vector();
colunmNames.add("编号");
colunmNames.add("姓名");
colunmNames.add("年龄");
colunmNames.add("性别");
}

/**
* 添加
*
* @param stu the stu
*/
public void insertStu( Student stu ){
try {
sDao.insertStudent(stu);
showAllData();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 删除
*
* @param id the id
*/
public void deleteStu( int id ){
try {
sDao.deleteStudent(id);
showAllData();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 更新
*
* @param stu the stu
*/
public void updateStu( Student stu ){
try {
sDao.updateStudent(stu);
showAllData();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 查询
*
* @param id the id
* @return the student
*/
public Student selectStuId( int id ){
Student stu = null;
try {
stu = sDao.selectStudentById(id);
Vector stuV = new Vector();
stuV.add(String.valueOf(stu.getStuId()));
stuV.add(stu.getStuName());
stuV.add(String.valueOf(stu.getStuAge()));
stuV.add(stu.getStuSex());

rowData.add(stuV);
} catch (SQLException e) {
e.printStackTrace();
}
return stu;
}

/**
* 显示所有
*/
public void showAllData(){
if( 0 != rowData.size() ){
rowData.clear();
}
try {
List<Student> stuList = sDao.selectStudentAll();
for( Student stu : stuList ){
Vector stuV = new Vector();
stuV.add(String.valueOf(stu.getStuId()));
stuV.add(stu.getStuName());
stuV.add(String.valueOf(stu.getStuAge()));
stuV.add(stu.getStuSex());

rowData.add(stuV);
}
} catch (SQLException e) {
e.printStackTrace();
}
}

/* (non-Javadoc)
* 设置表头
* @see javax.swing.table.AbstractTableModel#getColumnName(int)
*/
@Override
public String getColumnName(int column) {
return (String)colunmNames.get(column);
}

/* (non-Javadoc)
* 得到共有多少行
* @see javax.swing.table.TableModel#getRowCount()
*/
@Override
public int getRowCount() {
return rowData.size();
}

/* (non-Javadoc)
* 得到共有多少列
* @see javax.swing.table.TableModel#getColumnCount()
*/
@Override
public int getColumnCount() {
return colunmNames.size();
}

/* (non-Javadoc)
* 得到某行某列的数据
* @see javax.swing.table.TableModel#getValueAt(int, int)
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {

return ((Vector)rowData.get(rowIndex)).get(columnIndex);
}

/** The row data. */
private Vector rowData;

/** The colunm names. */
private Vector colunmNames;

/** The s dao. */
public static final StudentDao sDao = new StudentDaoImpl();
}


com.laolang.ui.StudentUpdateDialog

package com.laolang.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.laolang.domain.Student;

// TODO: Auto-generated Javadoc
/**
* 学生信息更新对话框 本类有一个 Student对象,用于记录各输入框中的值
*/
public class StudentUpdateDialog extends JDialog implements ActionListener {

/**
* Instantiates a new student insert dialog.
*/
public StudentUpdateDialog() {
super();
}

/**
* Instantiates a new student insert dialog.
*
* @param owner
*            the owner
* @param title
*            the title
* @param modal
*            the modal
* @param stu
*            the stu
*/
public StudentUpdateDialog(Frame owner, String title, boolean modal,
Student stu) {
super(owner, title, modal);
init(stu);
setComm();
setSize(300, 180);
setVisible(true);
setResizable(false);

}

/**
* 创建各组件
*
* @param s
*            the s
*/
private void init(Student s) {
stu = new Student();// 创建 Student对象
// 设置各属性值为选中行的相应的值
stu.setStuId(s.getStuId());
stu.setStuName(s.getStuName());
stu.setStuAge(s.getStuAge());
stu.setStuSex(s.getStuSex());
// 创建组件
labelId = new JLabel("编号");
labelName = new JLabel("姓名");
labelAge = new JLabel("年龄");
labelSex = new JLabel("性别");

tfId = new JTextField(String.valueOf(stu.getStuId()), 20);
tfId.setEditable(false);
tfName = new JTextField(stu.getStuName(), 20);
tfAge = new JTextField(String.valueOf(stu.getStuAge()), 20);
tfSex = new JTextField(stu.getStuSex(), 20);

butOk = new JButton("确定");
butCanel = new JButton("取消");

jpCenterLeft = new JPanel();
jpCenterRight = new JPanel();
jpCenter = new JPanel();
jpSouth = new JPanel();

// 设置布局
setLayout(new BorderLayout());
jpCenterLeft.setLayout(new GridLayout(6, 1));
jpCenterRight.setLayout(new GridLayout(6, 1));
jpCenter.setLayout(new FlowLayout());
jpSouth.setLayout(new FlowLayout());

// 添加组件到面板
jpCenterLeft.add(labelId);
jpCenterLeft.add(labelName);
jpCenterLeft.add(labelAge);
jpCenterLeft.add(labelSex);

jpCenterRight.add(tfId);
jpCenterRight.add(tfName);
jpCenterRight.add(tfAge);
jpCenterRight.add(tfSex);

jpCenter.add(jpCenterLeft);
jpCenter.add(jpCenterRight);

jpSouth.add(butOk);
jpSouth.add(butCanel);

// 面板添加到窗体
add(jpCenter);
add(jpSouth, BorderLayout.SOUTH);

}

/**
* 注册监听、设置命令.
*/
private void setComm() {
butOk.addActionListener(this);
butCanel.addActionListener(this);

butOk.setActionCommand("ok");
butCanel.setActionCommand("canel");
}

/*
* (non-Javadoc) 事件处理
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("ok")) {
// 修改Student对象的值
stu.setStuName(tfName.getText());
stu.setStuAge(Integer.valueOf(tfAge.getText()).intValue());
stu.setStuSex(tfSex.getText());
// 隐藏对话框
setVisible(false);

} else if (e.getActionCommand().equals("canel")) {
// 如果选择取消,则置Student对象为空
stu = null;
// 隐藏对话框
setVisible(false);
}
}

/** The label id. */
private JLabel labelId;

/** The label name. */
private JLabel labelName;

/** The label age. */
private JLabel labelAge;

/** The label sex. */
private JLabel labelSex;

/** The tf id. */
private JTextField tfId;

/** The tf name. */
private JTextField tfName;

/** The tf age. */
private JTextField tfAge;

/** The tf sex. */
private JTextField tfSex;

/** The but ok. */
private JButton butOk;

/** The but canel. */
private JButton butCanel;

/** The jp center left. */
private JPanel jpCenterLeft;

/** The jp center right. */
private JPanel jpCenterRight;

/** The jp center. */
private JPanel jpCenter;

/** The jp south. */
private JPanel jpSouth;

/** The stu. */
private Student stu;

/**
* Gets the stu.
*
* @return the stu
*/
public Student getStu() {
return stu;
}

}


运行效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: