Java实训第五次总结
一、创建APP包
创建Application
package net. wlm. student. app;
import net. wlm. student. gui. LoginFrame;
import net. wlm. student. gui. MainFrame;
import net. wlm. student. gui. RegisterFrame;
public class Application {
public static int id;
public static String username;
public static String password;
public static LoginFrame loginFrame;
public static MainFrame mainFrame;
public static RegisterFrame registerFrame;
}
二、创建gui包
1. 创建窗口界面类
1)创建LoginFrame
package net. wlm. student. gui;
import net. wlm. student. app. Application;
import net. wlm. student. bean. Status;
import net. wlm. student. bean. User;
import net. wlm. student. service. StatusService;
import net. wlm. student. service. UserService;
import net. wlm. student. service. impl. StatusServiceImpl;
import net. wlm. 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 LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JButton btnRegister;
private JPanel panel, panel1, panel2, panel3;
public LoginFrame ( String title) {
super ( title) ;
initGUI ( ) ;
}
public void initGUI ( ) {
panel = ( JPanel) getContentPane ( ) ;
panel1 = new JPanel ( ) ;
panel2 = new JPanel ( ) ;
panel3 = new JPanel ( ) ;
lblUsername = new JLabel ( "用户名:" ) ;
lblPassword = new JLabel ( "密 码:" ) ;
txtUsername = new JTextField ( 15 ) ;
txtPassword = new JPasswordField ( 15 ) ;
btnOK = new JButton ( "确定[O]" ) ;
btnCancel = new JButton ( "取消[C]" ) ;
btnRegister = new JButton ( "注册[R]" ) ;
panel1. add ( lblUsername) ;
panel1. add ( txtUsername) ;
panel2. add ( lblPassword) ;
panel2. add ( txtPassword) ;
panel3. add ( btnOK) ;
panel3. add ( btnCancel) ;
panel3. add ( btnRegister) ;
panel. setLayout ( new GridLayout ( 3 , 1 ) ) ;
panel. add ( panel1) ;
panel. add ( panel2) ;
panel. add ( panel3) ;
btnOK. setMnemonic ( KeyEvent. VK_O) ;
btnCancel. setMnemonic ( KeyEvent. VK_C) ;
btnRegister. setMnemonic ( KeyEvent. VK_R) ;
txtPassword. setEchoChar ( '*' ) ;
setSize ( 250 , 200 ) ;
setLocationRelativeTo ( null) ;
setResizable ( false ) ;
setVisible ( true ) ;
pack ( ) ;
setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
btnOK. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
login ( ) ;
}
} ) ;
btnOK. addKeyListener ( new KeyAdapter ( ) {
@Override
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == KeyEvent. VK_ENTER) {
login ( ) ;
}
}
} ) ;
txtUsername. addKeyListener ( new KeyAdapter ( ) {
@Override
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == KeyEvent. VK_DOWN) {
txtPassword. requestFocus ( ) ;
} }
} ) ;
txtPassword. addKeyListener ( new KeyAdapter ( ) {
@Override
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == KeyEvent. VK_ENTER) {
login ( ) ;
} }
} ) ;
btnCancel. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
System. exit ( 0 ) ;
}
} ) ;
btnRegister. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
Application. loginFrame. setVisible ( false ) ;
Application. registerFrame = new RegisterFrame ( "用户注册" ) ;
}
} ) ;
}
private void login ( ) {
username = txtUsername. getText ( ) . trim ( ) ;
password = new String ( txtPassword. getPassword ( ) ) ;
UserService userService = new UserServiceImpl ( ) ;
User user = userService. login ( username, password) ;
if ( user != null) {
Application. loginFrame. setVisible ( false ) ;
StatusService statusService = new StatusServiceImpl ( ) ;
Status status = statusService. findStatusById ( 1 ) ;
Application. id = user. getId ( ) ;
Application. username = user. getUsername ( ) ;
Application. password = user. getPassword ( ) ;
JOptionPane. showMessageDialog ( null,
"欢迎使用学生信息管理系统" + status. getVersion ( ) ,
"用户登录" , JOptionPane. INFORMATION_MESSAGE) ;
Application. mainFrame = new MainFrame ( "" ) ;
Application. loginFrame. dispose ( ) ;
} else {
Application. loginFrame. setVisible ( false ) ;
JOptionPane. showMessageDialog ( null,
"用户名或密码错误,请重新输入!" ,
"用户登录" , JOptionPane. ERROR_MESSAGE) ;
Application. loginFrame. setVisible ( true ) ;
txtUsername. selectAll ( ) ;
txtPassword. selectAll ( ) ;
txtUsername. requestFocus ( ) ;
}
}
public static void main ( String[ ] args) {
Application. loginFrame = new LoginFrame ( "用户登录" ) ;
}
}
效果图:
2)创建MainFrame
package net. wlm. student. gui;
import net. wlm. student. app. Application;
import net. wlm. student. bean. Status;
import net. wlm. student. service. StatusService;
import net. wlm. student. service. impl. StatusServiceImpl;
import javax. swing. *;
import java. awt. *;
import java. awt. event. *;
import java. io. IOException;
public class MainFrame extends JFrame {
private JMenuBar mnbMain;
private JMenu mnuSet;
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
private JMenu mnuOperate;
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
private JMenu mnuDelStu;
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
private JMenu mnuFind;
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
private JMenu mnuCount;
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
private JMenu mnuHelp;
private JMenuItem mniHelp;
private JMenuItem mniAbout;
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlSouth;
private JLabel lblStatusBar;
private JLabel lblBackground;
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
private JToolBar toolbar;
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCountByDepartment;
private Status status;
private StatusService statusService;
public MainFrame ( String title) {
super ( title) ;
initGUI ( ) ;
}
private void initGUI ( ) {
mnbMain = new JMenuBar ( ) ;
mnuSet = new JMenu ( "系统设置[S]" ) ;
mnuSet. setMnemonic ( KeyEvent. VK_S) ;
mniSetCollegeInfo = new JMenuItem ( "学校信息" ) ;
mniSetStatusBar = new JMenuItem ( "状态栏信息" ) ;
mniChangePassword = new JMenuItem ( "修改密码" ) ;
mniExit = new JMenuItem ( "退出系统" ) ;
mnuOperate = new JMenu ( "数据操作[O]" ) ;
mnuOperate. setMnemonic ( KeyEvent. VK_O) ;
mniAddStudent = new JMenuItem ( "增加学生表记录" ) ;
mnuDelStu = new JMenu ( "删除学生表记录" ) ;
mniEditStudent = new JMenuItem ( "编辑学生表记录" ) ;
mniBrowseStudent = new JMenuItem ( "浏览学生表记录" ) ;
mniDelStudentById = new JMenuItem ( "按学号删除" ) ;
mniDelStudentsByClass = new JMenuItem ( "按班级删除" ) ;
mniDelStudentsByDepartment = new JMenuItem ( "按系部删除" ) ;
mnuFind = new JMenu ( "查询学生[Q]" ) ;
mnuFind. setMnemonic ( KeyEvent. VK_Q) ;
mniFindStudentById = new JMenuItem ( "按学号查询" ) ;
mniFindStudentsByName = new JMenuItem ( "按姓名查询" ) ;
mniFindStudentsByClass = new JMenuItem ( "按班级查询" ) ;
mniFindStudentsByDepartment = new JMenuItem ( "按系部查询" ) ;
mnuCount = new JMenu ( "人数统计[C]" ) ;
mnuCount. setMnemonic ( KeyEvent. VK_C) ;
mniCountStudentsBySex = new JMenuItem ( "按性别统计" ) ;
mniCountStudentsByClass = new JMenuItem ( "按班级统计" ) ;
mniCountStudentsByDepartment = new JMenuItem ( "按系部统计" ) ;
mnuHelp = new JMenu ( "帮助[H]" ) ;
mnuHelp. setMnemonic ( KeyEvent. VK_H) ;
mniHelp = new JMenuItem ( "帮助" ) ;
mniAbout = new JMenuItem ( "关于" ) ;
imgCollege = new ImageIcon ( ".idea/images/college.png" ) ;
imgPassword = new ImageIcon ( ".idea/images/password.png" ) ;
imgQuery = new ImageIcon ( ".idea/images/query.png" ) ;
imgBrowse = new ImageIcon ( ".idea/images/browse.png" ) ;
imgCount = new ImageIcon ( ".idea/images/count.png" ) ;
imgExit = new ImageIcon ( ".idea/images/exit.png" ) ;
toolbar = new JToolBar ( ) ;
btnSetCollege = new JButton ( "设置学校" , imgCollege) ;
btnSetCollege. setToolTipText ( "设置学校信息" ) ;
btnSetCollege. setVerticalTextPosition ( AbstractButton. BOTTOM) ;
btnSetCollege. setHorizontalTextPosition ( AbstractButton. CENTER) ;
btnChangePassword = new JButton ( "修改密码" , imgPassword) ;
btnChangePassword. setToolTipText ( "修改用户密码" ) ;
btnChangePassword. setVerticalTextPosition ( AbstractButton. BOTTOM) ;
btnChangePassword. setHorizontalTextPosition ( AbstractButton. CENTER) ;
btnBrowseStudent = new JButton ( "浏览学生" , imgBrowse) ;
btnBrowseStudent. setToolTipText ( "浏览学生记录" ) ;
btnBrowseStudent. setVerticalTextPosition ( AbstractButton. BOTTOM) ;
btnBrowseStudent. setHorizontalTextPosition ( AbstractButton. CENTER) ;
btnFindStudentById = new JButton ( "查询学生" , imgQuery) ;
btnFindStudentById. setToolTipText ( "按学号查询学生记录" ) ;
btnFindStudentById. setVerticalTextPosition ( AbstractButton. BOTTOM) ;
btnFindStudentById. setHorizontalTextPosition ( AbstractButton. CENTER) ;
btnCountByDepartment = new JButton ( "统计人数" , imgCount) ;
btnCountByDepartment. setToolTipText ( "按系部统计学生人数" ) ;
btnCountByDepartment. setVerticalTextPosition ( AbstractButton. BOTTOM) ;
btnCountByDepartment. setHorizontalTextPosition ( AbstractButton. CENTER) ;
btnExit = new JButton ( "退出系统" , imgExit) ;
btnExit. setToolTipText ( "退出系统" ) ;
btnExit. setVerticalTextPosition ( AbstractButton. BOTTOM) ;
btnExit. setHorizontalTextPosition ( AbstractButton. CENTER) ;
toolbar. add ( btnSetCollege) ;
toolbar. add ( btnChangePassword) ;
toolbar. add ( btnBrowseStudent) ;
toolbar. add ( btnFindStudentById) ;
toolbar. add ( btnCountByDepartment) ;
toolbar. add ( btnExit) ;
panel = ( JPanel) getContentPane ( ) ;
pnlCenter = new JPanel ( ) ;
pnlSouth = new JPanel ( ) ;
pnlSouth. setLayout ( new FlowLayout ( FlowLayout. LEFT) ) ;
imgBackground = new ImageIcon ( ".idea/images/background.jpg" ) ;
lblBackground = new JLabel ( imgBackground) ;
lblStatusBar = new JLabel ( ) ;
setJMenuBar ( mnbMain) ;
mnbMain. add ( mnuSet) ;
mnuSet. add ( mniSetCollegeInfo) ;
mnuSet. add ( mniSetStatusBar) ;
mnuSet. add ( mniChangePassword) ;
mnuSet. addSeparator ( ) ;
mnuSet. add ( mniExit) ;
mnuDelStu. add ( mniDelStudentById) ;
mnuDelStu. add ( mniDelStudentsByClass) ;
mnuDelStu. add ( mniDelStudentsByDepartment) ;
mnbMain. add ( mnuOperate) ;
mnuOperate. add ( mniAddStudent) ;
mnuOperate. add ( mniEditStudent) ;
mnuOperate. add ( mnuDelStu) ;
mnuOperate. add ( mniBrowseStudent) ;
mnbMain. add ( mnuFind) ;
mnuFind. add ( mniFindStudentById) ;
mnuFind. add ( mniFindStudentsByName) ;
mnuFind. add ( mniFindStudentsByClass) ;
mnuFind. add ( mniFindStudentsByDepartment) ;
mnbMain. add ( mnuCount) ;
mnuCount. add ( mniCountStudentsBySex) ;
mnuCount. add ( mniCountStudentsByClass) ;
mnuCount. add ( mniCountStudentsByDepartment) ;
mnbMain. add ( mnuHelp) ;
mnuHelp. add ( mniHelp) ;
mnuHelp. add ( mniAbout) ;
panel. setLayout ( new BorderLayout ( ) ) ;
panel. add ( toolbar, "North" ) ;
panel. add ( pnlCenter, "Center" ) ;
panel. add ( pnlSouth, "South" ) ;
pnlCenter. add ( lblBackground) ;
pnlSouth. add ( lblStatusBar) ;
statusService = new StatusServiceImpl ( ) ;
status = statusService. findStatusById ( 1 ) ;
setSize ( 800 , 640 ) ;
setVisible ( true ) ;
setLocationRelativeTo ( null) ;
setTitle ( "学生信息管理系统" + status. getVersion ( ) ) ;
addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
exitSystem ( ) ;
}
} ) ;
mniSetCollegeInfo. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new SetCollegeInfoFrame ( "" ) ;
}
} ) ;
mniSetStatusBar. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new SetStatusBarFrame ( "" ) ;
}
} ) ;
mniChangePassword. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new ChangePasswordFrame ( "" ) ;
}
} ) ;
mniExit. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
exitSystem ( ) ;
}
} ) ;
mniFindStudentById. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new FindStudentByIdFrame ( "" ) ;
}
} ) ;
mniFindStudentsByName. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new FindStudentsByNameFrame ( "" ) ;
}
} ) ;
mniFindStudentsByClass. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new FindStudentsByClassFrame ( "" ) ;
}
} ) ;
mniFindStudentsByDepartment. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new FindStudentsByDepartmentFrame ( "" ) ;
}
} ) ;
mniCountStudentsBySex. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new CountStudentsBySexFrame ( "" ) ;
}
} ) ;
mniCountStudentsByClass. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new CountStudentsByClassFrame ( "" ) ;
}
} ) ;
mniCountStudentsByDepartment. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new CountStudentsByDepartmentFrame ( "" ) ;
}
} ) ;
mniAddStudent. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
new AddStudentFrame ( "" ) ;
}
} ) ;
mniDelStudentById. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new DeleteStudentByIdFrame ( "" ) ;
}
} ) ;
mniDelStudentsByClass. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new DeleteStudentsByClassFrame ( "" ) ;
}
} ) ;
mniDelStudentsByDepartment. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new DeleteStudentsByDepartmentFrame ( "" ) ;
}
} ) ;
mniEditStudent. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
new EditStudentFrame ( "" ) ;
}
} ) ;
mniBrowseStudent. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
new BrowseStudentsFrame ( "" ) ;
}
} ) ;
mniHelp. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
try {
Runtime. getRuntime ( ) . exec ( "cmd /c start help/帮助文档.chm" ) ;
} catch ( IOException e1) {
JOptionPane. showMessageDialog ( null, e1. getMessage ( ) , "学生信息管理系统" , JOptionPane. ERROR_MESSAGE) ;
}
}
} ) ;
mniAbout. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
JOptionPane. showMessageDialog ( null,
"开发人员:" + status. getCollege ( ) + "_" + status. getAuthor ( ) + "\n联系电话:" + status. getTelephone ( )
+ "\n电子邮箱:" + status. getEmail ( ) ,
"学生信息管理系统" + status. getVersion ( ) , JOptionPane. INFORMATION_MESSAGE) ;
}
} ) ;
btnSetCollege. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new SetCollegeInfoFrame ( "" ) ;
}
} ) ;
btnChangePassword. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new ChangePasswordFrame ( "" ) ;
}
} ) ;
btnBrowseStudent. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new BrowseStudentsFrame ( "" ) ;
}
} ) ;
btnFindStudentById. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new FindStudentByIdFrame ( "" ) ;
}
} ) ;
btnCountByDepartment. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
new CountStudentsByDepartmentFrame ( "" ) ;
}
} ) ;
btnExit. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent e) {
exitSystem ( ) ;
}
} ) ;
}
private void exitSystem ( ) {
int choice = JOptionPane. showConfirmDialog ( this ,
"您是否要退出系统?" , "学生信息管理系统" , JOptionPane. YES_NO_OPTION) ;
if ( choice == JOptionPane. YES_OPTION) {
System. exit ( 0 ) ;
} else {
dispose ( ) ;
Application. mainFrame = new MainFrame ( "学生信息管理系统" + status. getVersion ( ) ) ;
}
}
public void setStatusBar ( ) {
statusService = new StatusServiceImpl ( ) ;
status = statusService. findStatusById ( 1 ) ;
lblStatusBar. setText (
status. getCollege ( ) + "学生信息管理系统" + status. getVersion ( ) + " 作者:" + status. getAuthor ( ) + " 地址:"
+ status. getAddress ( ) + " 电话:" + status. getTelephone ( ) + " 邮箱:" + status. getEmail ( ) ) ;
}
public static void main ( String[ ] args) {
Application. mainFrame = new MainFrame ( "" ) ;
}
}
效果图:
3)创建RegisterFrame
package net. wlm. student. gui;
import net. wlm. student. app. Application;
import net. wlm. student. bean. User;
import net. wlm. student. service. UserService;
import net. wlm. student. service. impl. UserServiceImpl;
import javax. swing. *;
import java. awt. *;
import java. awt. event. *;
import java. sql. Timestamp;
import java. util. Date;
public class RegisterFrame extends JFrame {
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblTelephone;
private JTextField txtUsername;
private JTextField txtTelephone;
private JPasswordField txtPassword;
private JButton btnSubmit;
private JButton btnCancel;
private JButton btnLogin;
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private String username;
private String password;
private String telephone;
private Date registerTime;
public RegisterFrame ( String title) {
super ( title) ;
initGUI ( ) ;
}
private void initGUI ( ) {
panel = ( JPanel) getContentPane ( ) ;
panel1 = new JPanel ( ) ;
panel2 = new JPanel ( ) ;
panel3 = new JPanel ( ) ;
panel4 = new JPanel ( ) ;
lblUsername = new JLabel ( "用户名" ) ;
lblPassword = new JLabel ( "密 码" ) ;
lblTelephone = new JLabel ( "电 话" ) ;
txtUsername = new JTextField ( 15 ) ;
txtPassword = new JPasswordField ( 15 ) ;
txtTelephone= new JTextField ( 15 ) ;
btnSubmit = new JButton ( "提交[S]" ) ;
btnCancel = new JButton ( "取消[C]" ) ;
btnLogin = new JButton ( "登录[L]" ) ;
panel1. add ( lblUsername) ;
panel1. add ( txtUsername) ;
panel2. add ( lblPassword) ;
panel2. add ( txtPassword) ;
panel3. add ( lblTelephone) ;
panel3. add ( txtTelephone) ;
panel4. add ( btnSubmit) ;
panel4. add ( btnCancel) ;
panel4. add ( btnLogin) ;
panel. setLayout ( new GridLayout ( 4 , 1 ) ) ;
panel. add ( panel1) ;
panel. add ( panel2) ;
panel. add ( panel3) ;
panel. add ( panel4) ;
setSize ( 250 , 200 ) ;
setLocationRelativeTo ( null) ;
setResizable ( false ) ;
pack ( ) ;
setTitle ( "用户注册" ) ;
setVisible ( true ) ;
addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
dispose ( ) ;
Application. loginFrame. setVisible ( true ) ;
}
} ) ;
btnSubmit. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
register ( ) ;
}
} ) ;
btnSubmit. addKeyListener ( new KeyAdapter ( ) {
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == KeyEvent. VK_ENTER) {
register ( ) ;
}
}
} ) ;
btnCancel. addActionListener ( new ActionListener ( ) {
public void actionPerformed ( ActionEvent e) {
Application. loginFrame. setVisible ( true ) ;
dispose ( ) ;
}
} ) ;
btnLogin. addActionListener ( new ActionListener ( ) {
@Override
public void actionPerformed ( ActionEvent arg0) {
Application. registerFrame. setVisible ( false ) ;
Application. loginFrame = new LoginFrame ( "用户登录" ) ;
dispose ( ) ;
}
} ) ;
txtUsername. addKeyListener ( new KeyAdapter ( ) {
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == KeyEvent. VK_ENTER) ;
txtPassword. requestFocus ( ) ;
}
} ) ;
txtPassword. addKeyListener ( new KeyAdapter ( ) {
@Override
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == 10 ) {
txtTelephone. requestFocus ( ) ;
}
}
} ) ;
txtTelephone. addKeyListener ( new KeyAdapter ( ) {
@Override
public void keyPressed ( KeyEvent e) {
if ( e. getKeyCode ( ) == KeyEvent. VK_ENTER) {
btnSubmit. requestFocus ( ) ;
}
}
} ) ;
}
private void register ( ) {
username = txtUsername. getText ( ) . trim ( ) ;
password = new String ( txtPassword. getPassword ( ) ) ;
telephone = txtTelephone. getText ( ) . trim ( ) ;
registerTime = new Timestamp ( System. currentTimeMillis ( ) ) ;
if ( username. equals ( "" ) ) {
JOptionPane. showMessageDialog ( null, "用户名不能为空!" ,
"学生信息管理系统" , JOptionPane. WARNING_MESSAGE) ;
txtUsername. requestFocus ( ) ;
return ;
}
if ( password. equals ( "" ) ) {
JOptionPane. showMessageDialog ( null, "密码不能为空!" ,
"学生信息管理系统" , JOptionPane. WARNING_MESSAGE) ;
txtPassword. requestFocus ( ) ;
return ;
}
UserService userService = new UserServiceImpl ( ) ;
User user = new User ( ) ;
user. setUsername ( username) ;
user. setPassword ( password) ;
user. setTelephone ( telephone) ;
user. setRegisterTime ( registerTime) ;
int count = userService. addUser ( user) ;
if ( count > 0 ) {
setVisible ( false ) ;
JOptionPane. showMessageDialog ( null, "恭喜!注册成功!" ,
"学生信息管理系统" , JOptionPane. INFORMATION_MESSAGE) ;
setVisible ( true ) ;
} else {
JOptionPane. showMessageDialog ( null, "遗憾!注册失败!" ,
"学生信息管理系统" , JOptionPane. INFORMATION_MESSAGE) ;
}
}
public static void main ( String[ ] arge) {
Application. registerFrame = new RegisterFrame ( "用户注册" ) ;
}
}
效果图:
4)创建设置学校信息窗口SetCollegeInfoFrame
package net. wlm. student. gui;
import net. wlm. student. bean. College;
import net. wlm. student. service. CollegeService;
import net. wlm. 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 ;
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