实验五图形用户界面的设计与实现.docx
《实验五图形用户界面的设计与实现.docx》由会员分享,可在线阅读,更多相关《实验五图形用户界面的设计与实现.docx(18页珍藏版)》请在冰豆网上搜索。
实验五图形用户界面的设计与实现
实验报告
课程名称
面向对象程序设计
实验名称
实验五图形用户界面的设计与实现
日期
2017-11-9
学生学号
姓名
班级
B150404
实验目的:
通过本次实验,要求学生能进一步掌握图形用户界面的设计方法。
实验条件:
电脑一台、能上网查阅资料。
1.完成图书管理系统登陆界面和主界面的设计,完成图书管理系统的基本框架设计。
源代码:
数据库连接代码:
publicclassDbUtil{
privateStringdbUrl="jdbc:
mysql:
//localhost:
3306/db_book";//数据库连接地址
privateStringdbUserName="root";//用户名
privateStringdbPassword="123456";//密码
privateStringjdbcName="com.mysql.jdbc.Driver";//驱动名称
/**
*获取数据库连接
*@return
*@throwsException
*/
publicConnectiongetCon()throwsException{
Class.forName(jdbcName);
Connectioncon=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
returncon;
}
/**
*关闭数据库连接
*@paramcon
*@throwsException
*/
publicvoidcloseCon(Connectioncon)throwsException{
if(con!
=null){
con.close();
}
}
publicstaticvoidmain(String[]args){
DbUtildbUtil=newDbUtil();
try{
dbUtil.getCon();
System.out.println("数据库连接成功!
");
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
用户实体
/**
*用户实体
*@authorAdministrator
*
*/
publicclassUser{
privateintid;//编号
privateStringuserName;//用户名
privateStringpassword;//密码
publicUser(){
super();
//TODOAuto-generatedconstructorstub
}
publicUser(StringuserName,Stringpassword){
super();
this.userName=userName;
this.password=password;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
/**
*用户Dao类
*@authorAdministrator
*
*/
publicclassUserDao{
/**
*登录验证
*@paramcon
*@paramuser
*@return
*@throwsException
*/
publicUserlogin(Connectioncon,Useruser)throwsException{
UserresultUser=null;
Stringsql="select*fromt_userwhereuserName=?
andpassword=?
";
PreparedStatementpstmt=con.prepareStatement(sql);
pstmt.setString(1,user.getUserName());
pstmt.setString(2,user.getPassword());
ResultSetrs=pstmt.executeQuery();
if(rs.next()){
resultUser=newUser();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
returnresultUser;
}
}
publicclassJavaInterFrmextendsJInternalFrame{
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
Java1234InterFrmframe=newJava1234InterFrm();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createtheframe.
*/
publicJava1234InterFrm(){
getContentPane().setBackground(Color.RED);
setIconifiable(true);
setClosable(true);
setTitle("\u5173\u4E8EJava1234");
setBounds(100,100,450,300);
JLabellblNewLabel=newJLabel("");
lblNewLabel.setIcon(newImageIcon(Java1234InterFrm.class.getResource("/images/java1234.png")));
GroupLayoutgroupLayout=newGroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(98)
.addComponent(lblNewLabel)
.addContainerGap(126,Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(48)
.addComponent(lblNewLabel)
.addContainerGap(149,Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
}
}
登录界面的代码:
publicclassLogOnFrmextendsJFrame{
privateJPanelcontentPane;
privateJTextFielduserNameTxt;
privateJPasswordFieldpasswordTxt;
privateDbUtildbUtil=newDbUtil();
privateUserDaouserDao=newUserDao();
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
LogOnFrmframe=newLogOnFrm();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createtheframe.
*/
publicLogOnFrm(){
//改变系统默认字体
Fontfont=newFont("Dialog",Font.PLAIN,12);
java.util.Enumerationkeys=UIManager.getDefaults().keys();
while(keys.hasMoreElements()){
Objectkey=keys.nextElement();
Objectvalue=UIManager.get(key);
if(valueinstanceofjavax.swing.plaf.FontUIResource){
UIManager.put(key,font);
}
}
setResizable(false);
setTitle("\u7BA1\u7406\u5458\u767B\u5F55");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,343);
contentPane=newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));
setContentPane(contentPane);
JLabellblNewLabel=newJLabel("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF");
lblNewLabel.setFont(newFont("宋体",Font.BOLD,23));
lblNewLabel.setIcon(newImageIcon(LogOnFrm.class.getResource("/images/logo.png")));
JLabellblNewLabel_1=newJLabel("\u7528\u6237\u540D\uFF1A");
lblNewLabel_1.setIcon(newImageIcon(LogOnFrm.class.getResource("/images/userName.png")));
JLabellblNewLabel_2=newJLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_2.setIcon(newImageIcon(LogOnFrm.class.getResource("/images/password.png")));
userNameTxt=newJTextField();
userNameTxt.setColumns(10);
passwordTxt=newJPasswordField();
JButtonbtnNewButton=newJButton("\u767B\u5F55");
btnNewButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
loginActionPerformed(e);
}
});
btnNewButton.setIcon(newImageIcon(LogOnFrm.class.getResource("/images/login.png")));
JButtonbtnNewButton_1=newJButton("\u91CD\u7F6E");
btnNewButton_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
resetValueActionPerformed(e);
}
});
btnNewButton_1.setIcon(newImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
GroupLayoutgl_contentPane=newGroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(111)
.addComponent(lblNewLabel))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(101)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel_1)
.addComponent(lblNewLabel_2)
.addComponent(btnNewButton))
.addGap(32)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(btnNewButton_1)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING,false)
.addComponent(passwordTxt)
.addComponent(userNameTxt,GroupLayout.DEFAULT_SIZE,128,Short.MAX_VALUE)))))
.addContainerGap(111,Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(30)
.addComponent(lblNewLabel)
.addGap(26)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel_1)
.addGap(29)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_2)
.addComponent(passwordTxt,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)))
.addComponent(userNameTxt,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE))
.addGap(36)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton)
.addComponent(btnNewButton_1))
.addContainerGap(60,Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
//设置JFrame居中显示
this.setLocationRelativeTo(null);
}
/**
*登录事件处理
*@parame
*/
privatevoidloginActionPerformed(ActionEventevt){
StringuserName=this.userNameTxt.getText();
Stringpassword=newString(this.passwordTxt.getPassword());
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null,"用户名不能为空!
");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(null,"密码不能为空!
");
return;
}
Useruser=newUser(userName,password);
Connectioncon=null;
try{
con=dbUtil.getCon();
UsercurrentUser=userDao.login(con,user);
if(currentUser!
=null){
dispose();
newMainFrm().setVisible(true);
}else{
JOptionPane.showMessageDialog(null,"用户名或者密码错误!
");
}
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*重置事件处理
*@parame
*/
privatevoidresetValueActionPerformed(ActionEventevt){
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}
}
系统界面代码:
publicclassMainFrmextendsJFrame{
privateJPanelcontentPane;
privateJDesktopPanetable=null;
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
MainFrmframe=newMainFrm();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
*Createth