今天我完成了设置学校信息窗口SetCollegeInfoFrame,修改用户密码窗口ChangePasswordFrame,浏览学生窗口BrowseStudentsFrame,编辑学生记录ditStudentFrame
1、
package net.tr.student.gui;
/**
import net.tr.student.bean.College;
import net.tr.student.service.CollegeService;
import net.tr.student.service.impl.CollegeServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class SetCollegeInfoFrame extends JFrame {
/**
* 面板
/
JPanel panel;
JPanel pnlNorth;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JPanel panel5;
JPanel pnlCenter;
JPanel pnlSouth;
/*
* 标签
/
JLabel lblName;
JLabel lblPresident;
JLabel lblStartTime;
JLabel lblTelephone;
JLabel lblEmail;
JLabel lblAddress;
JLabel lblProfile;
/*
* 按钮
/
JButton btnSave;
JButton btnExit;
/*
* 文本框与文本区
/
JTextField txtName;
JTextField txtPresident;
JTextField txtStartTime;
JTextField txtTelephone;
JTextField txtEmail;
JTextField txtAddress;
JTextArea txtProfile;
/*
* 滚动面板
/
JScrollPane scrollPane;
/*
* 学校服务
*/
private CollegeService collegeService;
private College college;
private int id = 1;
/**
* 构造方法
*
* @param title
*/
public SetCollegeInfoFrame(String title){
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI(){
//创建组件(面板与控件)
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4= new JPanel();
panel5 = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
lblName = new JLabel("学校名称:");
lblPresident = new JLabel("校 长:");
lblStartTime = new JLabel("建校时间:");
lblTelephone = new JLabel("联系电话:");
lblEmail = new JLabel("电子邮箱:");
lblAddress = new JLabel("通讯地址:");
lblProfile = new JLabel("学校简介:");
txtName = new JTextField(30);
txtPresident = new JTextField(11);
txtStartTime = new JTextField(12);
txtTelephone = new JTextField(11);
txtEmail= new JTextField(12);
txtAddress = new JTextField(30);
txtProfile = new JTextArea(5,37);
//让文本区自动换行
txtProfile.setLineWrap(true);
scrollPane = new JScrollPane(txtProfile,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
btnSave = new JButton("保存[S]");
btnSave.setMnemonic(KeyEvent.VK_S);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
//设置大面板布局,将北面板,中面板与南面板添加到大面板
panel.setLayout(new BorderLayout());
panel.add(pnlNorth,"North");
panel.add(pnlCenter,"Center");
panel.add(pnlSouth,"South");
//设置北面板布局,添加五个小面板
pnlNorth.setLayout(new GridLayout(5,1));
pnlNorth.add(panel1);
pnlNorth.add(panel2);
pnlNorth.add(panel3);
pnlNorth.add(panel4);
pnlNorth.add(panel5);
//将控件依次添加到五个面板
panel1.add(lblName);
panel1.add(txtName);
panel2.add(lblPresident);
panel2.add(txtPresident);
panel2.add(lblStartTime);
panel2.add(txtStartTime);
panel3.add(lblTelephone);
panel3.add(txtTelephone);
panel3.add(lblEmail);
panel3.add(txtEmail);
panel4.add(lblAddress);
panel4.add(txtAddress);
panel5.add(lblProfile);
//将滚动面板添加到中面板
pnlCenter.add(scrollPane);
//将两个按钮添加到南面板
pnlSouth.add(btnSave);
pnlSouth.add(btnExit);
//创建学校服务对象
collegeService = new CollegeServiceImpl();
//获取学校对象
college = collegeService.findCollegeById(id);
//利用学校对象属性值设置相应控件的内容
txtName.setText(college.getName());
txtPresident.setText(college.getPresident());
txtStartTime.setText(college.getStartTime().toString());
txtTelephone.setText(college.getTelephone());
txtEmail.setText(college.getEmail());
txtAddress.setText(college.getAddress());
txtProfile.setText(college.getProfile());
//设置窗口尺寸
setSize(450, 350);
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口不可调整大小
setResizable(false);
//设置窗口刚好容纳组件
pack();
//设置窗口标题
setTitle("设置学校信息");
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//关闭按钮单击事件处理
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
//关闭窗口
dispose();
}
});
//保存按钮单击事件处理
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
try {
//修改学校属性
college.setName(txtName.getText());
college.setPresident(txtPresident.getText());
college.setStartTime(new Timestamp(
new SimpleDateFormat("yyyy-MM-dd")
.parse(txtStartTime.getText().toString()).getTime()));
college.setTelephone(txtTelephone.getText());
college.setEmail(txtEmail.getText());
college.setAddress(txtAddress.getText());
college.setProfile(txtProfile.getText());
//更新学校信息
collegeService.updateCollege(college);
//提示用户更新成功
JOptionPane.showMessageDialog(null,"更新数据成功!");
} catch (ParseException e1) {
//提示用户更新失败
JOptionPane.showMessageDialog(null,"更新数据失败!");
}
}
});
}
public static void main(String[] args){
new SetCollegeInfoFrame("");
}
}
2、
package net.tr.student.gui;
/**
import net.tr.student.app.Application;
import net.tr.student.bean.User;
import net.tr.student.service.UserService;
import net.tr.student.service.impl.UserServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class ChangePasswordFrame extends JFrame {
/**
* 面板
/
private JPanel panel;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
/*
* 标签
*/
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblNewPassword1;
private JLabel lblNewPassword2;
/**
* 文本框
*/
private JTextField txtUsername;
private JPasswordField txtPassword;
private JPasswordField txtNewPassword1;
private JPasswordField txtNewPassword2;
/**
* 按钮
*/
private JButton btnOK;
private JButton btnCancel;
/**
* 用户服务对象
*/
private UserService userService;
public ChangePasswordFrame(String title){
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI(){
//创建组件(面板与控件)
panel = (JPanel) getContentPane();
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("旧密码:");
lblNewPassword1 = new JLabel("新密码:");
lblNewPassword2 = new JLabel("确 认:");
txtUsername = new JTextField(20);
txtUsername.setEditable(false);
txtPassword = new JPasswordField(20);
txtNewPassword1 = new JPasswordField(20);
txtNewPassword2 = new JPasswordField(20);
btnOK = new JButton("确定[O]");
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel = new JButton("取消[C]");
btnCancel.setMnemonic(KeyEvent.VK_C);
//设置大面板布局,添加五个小面板
panel.setLayout(new GridLayout(5,1));
panel.add(pnlRow1);
panel.add(pnlRow2);
panel.add(pnlRow3);
panel.add(pnlRow4);
panel.add(pnlRow5);
//将控件添加到五个小面板
pnlRow1.add(lblUsername);
pnlRow1.add(txtUsername);
pnlRow2.add(lblPassword);
pnlRow2.add(txtPassword);
pnlRow3.add(lblNewPassword1);
pnlRow3.add(txtNewPassword1);
pnlRow4.add(lblNewPassword2);
pnlRow4.add(txtNewPassword2);
pnlRow5.add(btnOK);
pnlRow5.add(btnCancel);
//设置窗口不可调整大小
setResizable(false);
//设置窗口刚好容纳组件
pack();
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口标题
setTitle("修改用户密码");
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置控件属性
txtUsername.setEditable(false);
txtUsername.setText(Application.username);
//取消按钮单击事件处理
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//确定按钮单击事件处理
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changePassword();
}
});
//确定按钮按键事件处理
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10){
changePassword();
}
}
});
//用户名文本框按键事件处理
txtUsername.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
txtPassword.requestFocus();
}
}
});
//新密码1文本框按键事件处理
txtNewPassword1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
txtNewPassword2.requestFocus();
}
}
});
//新密码2文本框按键事件处理
txtNewPassword2.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() ==KeyEvent.VK_ENTER){
btnOK.requestFocus();
}
}
});
}
private void changePassword(){
//获取用户信息
int id = Application.id;
String username = Application.username;
//保存用户输入的旧密码与两个新密码
String password = new String(txtPassword.getPassword());
String newPassword1 = new String(txtNewPassword1.getPassword());
String newPassword2 = new String(txtNewPassword1.getPassword());
//创建学生服务对象
userService = new UserServiceImpl();
if (userService.login(username,password)== null) {
JOptionPane.showMessageDialog(null, "旧密码错误,请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtPassword.requestFocus();
txtPassword.selectAll();
}else if(newPassword1.equals("")) {
JOptionPane.showMessageDialog(null, "新密码不能为空!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtNewPassword1.requestFocus();
}else if (newPassword2.equals("")) {
JOptionPane.showMessageDialog(null, "确认密码不能为空!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtNewPassword2.requestFocus();
}else if (!newPassword1.equals(newPassword2)) {
JOptionPane.showMessageDialog(null, "两次密码不一致,请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtNewPassword1.setText("");
txtNewPassword2.setText("");
txtNewPassword1.requestFocus();
}else {
//按标识符获取用户
User user = userService.findUserById(id);
//修改用户的密码属性
user.setPassword(newPassword1);
//调用用户服务对象的更新方法,更新用户信息
int count = userService.updateUser(user);
if (count > 0) {
JOptionPane.showMessageDialog(null, "密码修改成功!", "设置密码", JOptionPane.INFORMATION_MESSAGE);
dispose();
}else {
JOptionPane.showMessageDialog(null,"密码修改失败!","设置密码",JOptionPane.WARNING_MESSAGE);
}
}
}
public static void main(String[] args){
Application.id = 1;
Application.username = "李刚";
new ChangePasswordFrame("");
}
}
3、
package net.tr.student.gui;
/**
import net.tr.student.bean.Student;
import net.tr.student.service.StudentService;
import net.tr.student.service.impl.StudentServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
public class BrowseStudentsFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/**
* 声明标签
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 创建学生服务对象
*/
StudentService studentService;
public BrowseStudentsFrame(String title){
super(title);
intiGUI();
}
/**
* 初始化用户界面
*/
private void intiGUI(){
//创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId = new JLabel("学号:");
lblName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge= new JLabel("年龄:");
lblDepartment = new JLabel("系部:");
lblClass= new JLabel("班级:");
lblTelephone = new JLabel("电话:");
txtId = new JTextField(40);
txtName= new JTextField(40);
txtSex = new JTextField(40);
txtAge = new JTextField(40);
txtDepartment = new JTextField(40);
txtClass = new JTextField(40);
txtTelephone = new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
btnTop = new JButton("第一条[T]");
btnPrevious = new JButton("上一条[P]");
btnNext = new JButton("下一条[N]");
btnBottom = new JButton("最后一条[B]");
btnExit = new JButton("退出[X]");
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_T);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);
//添加主键
panel.setLayout(new BorderLayout());
panel.add(pnlCenter,BorderLayout.CENTER);
panel.add(pnlSouth,BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7,1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(btnTop);
pnlSouth.add(btnPrevious);
pnlSouth.add(btnNext);
pnlSouth.add(btnBottom);
pnlSouth.add(btnExit);
//设置窗口属性
setSize(500,300);
//设置窗口不可调整大小
setResizable(false);
//设置窗口刚好容纳组件
pack();
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建学生服务对象
studentService = new StudentServiceImpl();
//获取全部学生列表
students = studentService.findAllStudents();
//判断是否有学生记录
if (students.size()>0) {
//设置当前记录号
currentRow = 1;
//设置窗口标题
setTitle("浏览学生表记录" + "&& 当前记录:" + currentRow);
//填充窗口各文本框数据
fillFrameData(currentRow);
}else {
JOptionPane.showMessageDialog(null,"表中没有记录","浏览学生表记录",JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}
//第一条按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow = 1;
//填充当前记录数据
fillFrameData(currentRow);
}
});
//上一条按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow>1) {
//设置当前记录号
currentRow--;
//填充当前记录数据
fillFrameData(currentRow);
}else{
JOptionPane.showMessageDialog(null,"已到第一条记录","浏览学生表记录!",JOptionPane.WARNING_MESSAGE);
}
}
});
//下一条按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow < students.size() -1) {
//设置当前记录号
currentRow++;
//填充当前记录数据
fillFrameData(currentRow);
}else {
JOptionPane.showMessageDialog(null,"已到最后一条记录","浏览学生表记录!",JOptionPane.WARNING_MESSAGE);
}
}
});
//最后一条按钮单击事件
btnBottom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow = students.size()-1;
//填充当前记录数据
fillFrameData(currentRow);
}
});
//退出按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
private void fillFrameData(int currentRow) {
if (currentRow > 0) {
setTitle("浏览学生表记录" + "&& 当前记录" + currentRow);
txtId.setText(students.get(currentRow).getId());
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
}
public static void main(String[] args){
new BrowseStudentsFrame("");
}
}
4、package net.tr.student.gui;
/**
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EditStudentFrame extends JFrame {
/**
* 声明面板
/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
private JPanel pnlSouth1;
private JPanel pnlSouth2;
/*
* 声明标签
/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/*
* 声明文本框
/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/*
* 声明按钮
/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
private JButton btnEdit;
private JButton btnOK;
private JButton btnCancel;
/*
* 当前记录行号
/
private int currentRow;
/*
* 学生列表
/
private List students;
/*
* 创建学生服务对象
*/
StudentService studentService;
public EditStudentFrame(String title){
super(title);
initGUI();
}
/**
* 初始化界面
*/
private void initGUI(){
//创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new GridLayout(2,1));
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();
pnlSouth1 = new JPanel();
pnlSouth2 = new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId = new JLabel("学号:");
lblName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge= new JLabel("年龄:");
lblDepartment = new JLabel("系部:");
lblClass= new JLabel("班级:");
lblTelephone = new JLabel("电话:");
txtId = new JTextField(40);
txtName= new JTextField(40);
txtSex = new JTextField(40);
txtAge = new JTextField(40);
txtDepartment = new JTextField(40);
txtClass = new JTextField(40);
txtTelephone = new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
btnTop = new JButton("第一条[T]");
btnPrevious = new JButton("上一条[P]");
btnNext = new JButton("下一条[N]");
btnBottom = new JButton("最后一条[B]");
btnExit = new JButton("退出[X]");
btnEdit = new JButton("编辑[E]");
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_P);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);
btnEdit.setMnemonic(KeyEvent.VK_E);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
//添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter,BorderLayout.CENTER);
panel.add(pnlSouth,BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7,1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(pnlSouth1);
pnlSouth.add(pnlSouth2);
pnlSouth1.add(btnTop);
pnlSouth1.add(btnPrevious);
pnlSouth1.add(btnNext);
pnlSouth1.add(btnBottom);
pnlSouth1.add(btnExit);
pnlSouth2.add(btnEdit);
pnlSouth2.add(btnOK);
pnlSouth2.add(btnCancel);
//设置窗口属性
setSize(500,300);
//设置窗口不可调整大小
setResizable(false);
//设置窗口刚好容纳组件
pack();
//设置窗口屏幕居中
setLocationRelativeTo(null);
//设置窗口可见
setVisible(true);
//设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建学生服务对象
studentService = new StudentServiceImpl();
//获取全部学生列表
students = studentService.findAllStudents();
//判断是否有学生记录
if (students.size()>0) {
//设置当前记录号
currentRow = 1;
//设置窗口标题
setTitle("浏览学生表记录" + "&& 当前记录:" + currentRow);
//填充窗口各文本框数据
fillFrameData(currentRow);
}else {
JOptionPane.showMessageDialog(null,"表中没有记录!","浏览学生表记录",JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}
//第一条按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow = 1;
//填充当前记录数据
fillFrameData(currentRow);
}
});
//上一条按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow>1) {
//设置当前记录号
currentRow--;
//填充当前记录数据
fillFrameData(currentRow);
}else{
JOptionPane.showMessageDialog(null,"已到第一条记录","浏览学生表记录!",JOptionPane.WARNING_MESSAGE);
}
}
});
//下一条按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow < students.size() - 1) {
//设置当前记录号
currentRow++;
//填充当前记录数据
fillFrameData(currentRow);
}else {
JOptionPane.showMessageDialog(null,"已到最后一条记录","浏览学生表记录!",JOptionPane.WARNING_MESSAGE);
}
}
});
//最后一条按钮单击事件
btnBottom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置当前记录号
currentRow = students.size()-1;
//填充当前记录数据
fillFrameData(currentRow);
}
});
//退出按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
//编辑按钮单击事件
btnEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//设置窗口标题
setTitle("编辑学生表记录" + "&& 当前记录" + currentRow);
txtName.setEditable(true);
txtSex.setEditable(true);
txtAge.setEditable(true);
txtDepartment.setEditable(true);
txtClass.setEditable(true);
txtTelephone.setEditable(true);
btnOK.setEnabled(true);
btnCancel.setEnabled(true);
}
});
//确定按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取当前学生实体
Student student = students.get(currentRow);
if (isNumber(txtAge.getText())) {
if (isLegalTelephone(txtTelephone.getText())) {
//修改学生实体属性
student.setName(txtName.getText());
student.setSex(txtSex.getText());
student.setAge(Integer.parseInt(txtAge.getText()));
student.setDepartment(txtDepartment.getText());
student.setClazz(txtClass.getText());
student.setTelephone(txtTelephone.getText());
//更新学生记录
int count = studentService.updateStudent(student);
//判断是否更新成功
if (count > 0) {
JOptionPane.showMessageDialog(null, "更新记录成功!", "编辑学生记录", JOptionPane.INFORMATION_MESSAGE);
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnEdit.setEnabled(true);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
//重新获取全部学生列表
students = studentService.findAllStudents();
//设置窗口标题
setTitle("浏览学生表记录" + "&& 当前记录" + currentRow);
} else {
JOptionPane.showMessageDialog(null, "更新记录失败!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "非法手机号!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
txtTelephone.selectAll();
txtTelephone.requestFocus();
}
}else {
JOptionPane.showMessageDialog(null, "年龄必须是数字!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
txtAge.selectAll();
txtAge.requestFocus();
}
}
});
//取消按钮单击事件
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnEdit.setEnabled(true);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
//恢复文本框修改前的值
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
});
//学号文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
txtName.requestFocus();
}
}
});
//姓名文本框按键事件
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtSex.requestFocus();
}
}
});
//性别文本框按键事件
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtAge.requestFocus();
}
}
});
//年龄文本框按键事件
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtDepartment.requestFocus();
}
}
});
//系部文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtClass.requestFocus();
}
}
});
//班级文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtTelephone.requestFocus();
}
}
});
}
private void fillFrameData(int currentRow){
if (currentRow > 0){
setTitle("浏览学生表记录" + "&& 当前记录"+currentRow);
txtId.setText(students.get(currentRow).getId());
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
}
//判断一个字符串是否全是数字
private boolean isNumber(String str){
for (int i = 0; i < str.length();i++){
if (str.charAt(i) < '0'|| str.charAt(i) > '9'){
return false;
}
}
return true;
}
private boolean isLegalTelephone(String telephone){
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(telephone);
return m.matches();
}
public static void main(String[] args){
new EditStudentFrame("");
}
}