欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

图书管理系统java代码实现

程序员文章站 2023-12-13 18:54:16
本文实例为大家分享了java实现图书管理系统的具体代码,供大家参考,具体内容如下 /* (程序头部注释开始) * 程序的版权和版本声明部分 * copyright (...

本文实例为大家分享了java实现图书管理系统的具体代码,供大家参考,具体内容如下

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* copyright (c) 2011, 烟台大学计算机学院学生
* all rights reserved.
* 文件名称:    《图书管理系统——java》                         
* 作    者:       刘江波                      
* 完成日期:    2012     年  3     月     1   日
* 版 本 号:    v3.0    

* 对任务及求解方法的描述部分
* 问题描述: 
* 程序头部的注释结束
*/

文件包的建立情况:

图书管理系统java代码实现

bookdao.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
package com.liu.dao; 
 
import com.liu.po.bookbean; 
import java.io.*; 
import java.util.hashmap; 
import java.util.map; 
import java.util.logging.level; 
import java.util.logging.logger; 
 
/** 
 * 
 * @author asus 
 */ 
public class bookdao { 
 // 写 
 public void writebook(map<integer,bookbean >bookmap){ 
 // 
 fileoutputstream fos = null; 
 objectoutputstream oos = null; 
 try { 
  fos = new fileoutputstream("f:\\缓存区\\book.txt"); 
  oos = new objectoutputstream(fos); 
  oos.writeobject(bookmap); 
  //清空缓存区 
  oos.flush(); 
 } 
 catch (filenotfoundexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } 
 //异常级别高的在后边 
 catch (ioexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } 
 finally{ 
  try{ 
  //先开后闭 
   oos.close(); 
   fos.close(); 
  }catch(ioexception ex){ 
  logger.getlogger(typedao.class.getname()).log(level.severe,null,ex); 
  } 
 } 
 } 
 
 //读 
 public map<integer,bookbean>readbook(){ 
 fileinputstream fis = null; 
 objectinputstream ois = null; 
 map<integer, bookbean> map = null; 
 try { 
  fis = new fileinputstream("f:\\缓存区\\book.txt"); 
  ois = new objectinputstream(fis); 
  map = (map<integer, bookbean>) ois.readobject();//出现异常进入catch 
 } catch (classnotfoundexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } catch (filenotfoundexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } catch (ioexception ex) { 
  //logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
  //ex.printstacktrace(); 
  map=new hashmap<integer,bookbean>();//出现异常时,进行创建map 
 } finally{ 
  try { 
  if(ois!=null){ 
   ois.close(); 
  } 
  if(fis!=null){ 
   fis.close(); 
  } 
 
  } catch (ioexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
  } 
 } 
 return map; 
 } 
} 

 typedao.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
package com.liu.dao; 
 
import com.liu.po.typebean; 
import java.io.*; 
import java.util.*; 
import java.util.logging.*; 
/** 
 * 
 * 对文件进行读和写操作 
 */ 
public class typedao { 
 
 // 写 
 public void writetype(map<integer,typebean >typemap){ 
 // 
 fileoutputstream fos = null; 
 objectoutputstream oos = null; 
 try { 
  fos = new fileoutputstream("f:\\缓存区\\type.txt"); 
  oos = new objectoutputstream(fos); 
  oos.writeobject(typemap); 
  //清空缓存区 
  oos.flush(); 
 } 
 catch (filenotfoundexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } 
 //异常级别高的在后边 
 catch (ioexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } 
 finally{ 
  try{ 
  //先开后闭 
   oos.close(); 
   fos.close(); 
  }catch(ioexception ex){ 
  logger.getlogger(typedao.class.getname()).log(level.severe,null,ex); 
  } 
 } 
 } 
 
 //读 
 public map<integer,typebean>readtype(){ 
 fileinputstream fis = null; 
 objectinputstream ois = null; 
 map<integer, typebean> map = null; 
 try { 
  fis = new fileinputstream("f:\\缓存区\\type.txt"); 
  ois = new objectinputstream(fis); 
  map = (map<integer, typebean>) ois.readobject();//出现异常进入catch 
 } catch (classnotfoundexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } catch (filenotfoundexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
 } catch (ioexception ex) { 
  //logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
  //ex.printstacktrace(); 
  map=new hashmap<integer,typebean>();//出现异常时,进行创建map 
 } finally{ 
  try { 
  if(ois!=null){ 
   ois.close(); 
  } 
  if(fis!=null){ 
   fis.close(); 
  } 
   
  } catch (ioexception ex) { 
  logger.getlogger(typedao.class.getname()).log(level.severe, null, ex); 
  } 
 } 
 return map; 
 } 
} 

bookbean.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
package com.liu.po; 
 
import java.io.serializable; 
 
/** 
 * 
 * @author asus 
 */ 
public class bookbean implements serializable{ 
 
 private int id; 
 private string bookname; 
 private string booktype; 
 private string memo; 
 private string money; 
 
 /** 
 * @return the id 
 */ 
 public int getid() { 
 return id; 
 } 
 
 /** 
 * @param id the id to set 
 */ 
 public void setid(int id) { 
 this.id = id; 
 } 
 
 /** 
 * @return the bookname 
 */ 
 public string getbookname() { 
 return bookname; 
 } 
 
 /** 
 * @param bookname the bookname to set 
 */ 
 public void setbookname(string bookname) { 
 this.bookname = bookname; 
 } 
 
 /** 
 * @return the booktype 
 */ 
 public string getbooktype() { 
 return booktype; 
 } 
 
 /** 
 * @param booktype the booktype to set 
 */ 
 public void setbooktype(string booktype) { 
 this.booktype = booktype; 
 } 
 
 /** 
 * @return the memo 
 */ 
 public string getmemo() { 
 return memo; 
 } 
 
 /** 
 * @param memo the memo to set 
 */ 
 public void setmemo(string memo) { 
 this.memo = memo; 
 } 
 
 /** 
 * @return the money 
 */ 
 public string getmoney() { 
 return money; 
 } 
 
 /** 
 * @param money the money to set 
 */ 
 public void setmoney(string money) { 
 this.money = money; 
 } 
} 

typebean.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
package com.liu.po; 
 
/** 
 * 
 * @author asus 
 */ 
import java.io.serializable; 
 
public class typebean implements serializable{ 
 
 private int id; 
 private string typename; 
 private string memo; 
 
 /** 
 * @return the id 
 */ 
 public int getid() { 
 return id; 
 } 
 
 /** 
 * @param id the id to set 
 */ 
 public void setid(int id) { 
 this.id = id; 
 } 
 
 /** 
 * @return the typename 
 */ 
 public string gettypename() { 
 return typename; 
 } 
 
 /** 
 * @param typename the typename to set 
 */ 
 public void settypename(string typename) { 
 this.typename = typename; 
 } 
 
 /** 
 * @return the memo 
 */ 
 public string getmemo() { 
 return memo; 
 } 
 
 /** 
 * @param memo the memo to set 
 */ 
 public void setmemo(string memo) { 
 this.memo = memo; 
 } 
 
 
} 

loginform.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
/* 
 * loginform.java 
 * 
 * created on 2013-2-26, 18:33:36 
 */ 
 
package com.liu.view; 
 
import java.awt.event.keyadapter; 
import java.awt.event.keyevent; 
import javax.swing.joptionpane; 
 
/** 
 * 
 * @author asus 
 */ 
public class loginform extends javax.swing.jframe { 
 
 /** creates new form loginform */ 
 public loginform() { 
 initcomponents(); 
 } 
 
 /** this method is called from within the constructor to 
 * initialize the form. 
 * warning: do not modify this code. the content of this method is 
 * always regenerated by the form editor. 
 */ 
 @suppresswarnings("unchecked") 
 // <editor-fold defaultstate="collapsed" desc="generated code">    
 private void initcomponents() { 
 
 jlabel1 = new javax.swing.jlabel(); 
 jlabel2 = new javax.swing.jlabel(); 
 jlabel3 = new javax.swing.jlabel(); 
 loginname = new javax.swing.jtextfield(); 
 loginpwd = new javax.swing.jpasswordfield(); 
 jbutton1 = new javax.swing.jbutton(); 
 
 setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); 
 settitle("登陆界面"); 
 setbounds(new java.awt.rectangle(300, 200, 0, 0)); 
 seticonimage(new javax.swing.imageicon(getclass().getresource("/com/liu/resouce/logo.jpg")).getimage()); 
 addkeylistener(new java.awt.event.keyadapter() { 
  public void keypressed(java.awt.event.keyevent evt) { 
  formkeypressed(evt); 
  } 
 }); 
 
 jlabel1.setfont(new java.awt.font("宋体", 0, 36)); 
 jlabel1.setforeground(new java.awt.color(204, 0, 0)); 
 jlabel1.settext("图书管理系统"); 
 
 jlabel2.setfont(new java.awt.font("宋体", 0, 24)); 
 jlabel2.settext("用户名:"); 
 
 jlabel3.setfont(new java.awt.font("宋体", 0, 24)); 
 jlabel3.settext("密码:"); 
 
 loginname.setname(""); // noi18n 
 loginname.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  loginnameactionperformed(evt); 
  } 
 }); 
 loginname.addkeylistener(new java.awt.event.keyadapter() { 
  public void keypressed(java.awt.event.keyevent evt) { 
  loginnamekeypressed(evt); 
  } 
 }); 
 
 loginpwd.addkeylistener(new java.awt.event.keyadapter() { 
  public void keypressed(java.awt.event.keyevent evt) { 
  loginpwdkeypressed(evt); 
  } 
 }); 
 
 jbutton1.setfont(new java.awt.font("宋体", 0, 24)); // noi18n 
 jbutton1.settext("登录"); 
 jbutton1.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton1actionperformed(evt); 
  } 
 }); 
 
 javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); 
 getcontentpane().setlayout(layout); 
 layout.sethorizontalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(layout.createsequentialgroup() 
  .addgap(97, 97, 97) 
  .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
   .addcomponent(jlabel2) 
   .addcomponent(jlabel3)) 
  .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) 
  .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false) 
   .addcomponent(loginpwd) 
   .addcomponent(loginname, javax.swing.grouplayout.default_size, 215, short.max_value)) 
  .addcontainergap(88, short.max_value)) 
  .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() 
  .addcontainergap(130, short.max_value) 
  .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 263, javax.swing.grouplayout.preferred_size) 
  .addgap(113, 113, 113)) 
  .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() 
  .addcontainergap(299, short.max_value) 
  .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 97, javax.swing.grouplayout.preferred_size) 
  .addgap(110, 110, 110)) 
 ); 
 layout.setverticalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(layout.createsequentialgroup() 
  .addgap(50, 50, 50) 
  .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 45, javax.swing.grouplayout.preferred_size) 
  .addgap(38, 38, 38) 
  .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel2) 
   .addcomponent(loginname, javax.swing.grouplayout.preferred_size, 29, javax.swing.grouplayout.preferred_size)) 
  .addgap(26, 26, 26) 
  .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel3) 
   .addcomponent(loginpwd, javax.swing.grouplayout.preferred_size, 29, javax.swing.grouplayout.preferred_size)) 
  .addgap(18, 18, 18) 
  .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 36, javax.swing.grouplayout.preferred_size) 
  .addcontainergap(21, short.max_value)) 
 ); 
 
 pack(); 
 }// </editor-fold>    
 
 private void loginnameactionperformed(java.awt.event.actionevent evt) {      
 // todo add your handling code here: 
}      
 
 private void jbutton1actionperformed(java.awt.event.actionevent evt) {      
  
 //1.先获取用户名和密码 
 string name = loginname.gettext(); 
 string password = new string(loginpwd.getpassword()); 
 //2.进行验证 
 if("admin".equals(name)&&"admin".equals(password)) 
 { 
  //登陆成功 
  //隐藏loginform,显示mainform 
  this.setvisible(false); 
  new mainform().setvisible(true); 
 } 
 else 
 { 
  //登录失败 
  joptionpane.showmessagedialog(this, "用户名或密码错误!"); 
 } 
 
 }      
 
 private void formkeypressed(java.awt.event.keyevent evt) {     
 //敲击键盘登陆 
  
 }     
 
 private void loginnamekeypressed(java.awt.event.keyevent evt) {     
 //敲击键盘登陆 
 if(evt.getkeytext(evt.getkeycode()).comparetoignorecase("enter")==0) 
 { 
  jbutton1.doclick(); 
 } 
 }     
 
 private void loginpwdkeypressed(java.awt.event.keyevent evt) {     
 //敲击键盘登陆 
 if(evt.getkeytext(evt.getkeycode()).comparetoignorecase("enter")==0) 
 { 
  jbutton1.doclick(); 
 } 
 }     
 
 /** 
 * @param args the command line arguments 
 */ 
 public static void main(string args[]) { 
 java.awt.eventqueue.invokelater(new runnable() { 
  public void run() { 
  new loginform().setvisible(true); 
  } 
 }); 
 } 
 
 // variables declaration - do not modify   
 private javax.swing.jtextfield loginname; 
 private javax.swing.jpasswordfield loginpwd; 
 private javax.swing.jbutton jbutton1; 
 private javax.swing.jlabel jlabel1; 
 private javax.swing.jlabel jlabel2; 
 private javax.swing.jlabel jlabel3; 
 // end of variables declaration   
 
} 

图书管理系统java代码实现

mainform.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
/* 
 * mainform.java 
 * 
 * created on 2013-2-26, 18:35:25 
 */ 
 
package com.liu.view; 
 
/** 
 * 
 * @author asus 
 */ 
public class mainform extends javax.swing.jframe { 
 
 /** creates new form mainform */ 
 public mainform() { 
 initcomponents(); 
  
 } 
 
 /** this method is called from within the constructor to 
 * initialize the form. 
 * warning: do not modify this code. the content of this method is 
 * always regenerated by the form editor. 
 */ 
 @suppresswarnings("unchecked") 
 // <editor-fold defaultstate="collapsed" desc="generated code">    
 private void initcomponents() { 
 
 jlabel1 = new javax.swing.jlabel(); 
 jpanel1 = new javax.swing.jpanel(); 
 jlabel2 = new javax.swing.jlabel(); 
 jmenubar1 = new javax.swing.jmenubar(); 
 配置管理 = new javax.swing.jmenu(); 
 jmenuitem1 = new javax.swing.jmenuitem(); 
 jmenuitem2 = new javax.swing.jmenuitem(); 
 jmenu2 = new javax.swing.jmenu(); 
 jmenuitem4 = new javax.swing.jmenuitem(); 
 jmenu1 = new javax.swing.jmenu(); 
 jmenuitem3 = new javax.swing.jmenuitem(); 
 
 setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); 
 settitle("图书管理系统"); 
 setbounds(new java.awt.rectangle(80, 60, 0, 0)); 
 seticonimage(new javax.swing.imageicon(getclass().getresource("/com/liu/resouce/logo.jpg")).getimage()); 
 
 jlabel1.setfont(new java.awt.font("宋体", 0, 48)); 
 jlabel1.setforeground(new java.awt.color(0, 204, 51)); 
 jlabel1.seticon(new javax.swing.imageicon(getclass().getresource("/com/liu/resouce/main.jpg"))); // noi18n 
 
 jlabel2.setfont(new java.awt.font("宋体", 0, 48)); 
 jlabel2.setforeground(new java.awt.color(0, 0, 255)); 
 jlabel2.settext("欢迎使用图书借阅管理系统"); 
 
 javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); 
 jpanel1.setlayout(jpanel1layout); 
 jpanel1layout.sethorizontalgroup( 
  jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel1layout.createsequentialgroup() 
  .addgap(38, 38, 38) 
  .addcomponent(jlabel2) 
  .addcontainergap(63, short.max_value)) 
 ); 
 jpanel1layout.setverticalgroup( 
  jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(javax.swing.grouplayout.alignment.trailing, jpanel1layout.createsequentialgroup() 
  .addcontainergap() 
  .addcomponent(jlabel2, javax.swing.grouplayout.default_size, 78, short.max_value)) 
 ); 
 
 配置管理.settext("配置管理"); 
 
 jmenuitem1.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_l, java.awt.event.inputevent.ctrl_mask)); 
 jmenuitem1.settext("类别管理"); 
 jmenuitem1.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jmenuitem1actionperformed(evt); 
  } 
 }); 
 配置管理.add(jmenuitem1); 
 
 jmenuitem2.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_t, java.awt.event.inputevent.ctrl_mask)); 
 jmenuitem2.settext("图书管理"); 
 jmenuitem2.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jmenuitem2actionperformed(evt); 
  } 
 }); 
 配置管理.add(jmenuitem2); 
 
 jmenubar1.add(配置管理); 
 
 jmenu2.settext("借书"); 
 
 jmenuitem4.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_j, java.awt.event.inputevent.ctrl_mask)); 
 jmenuitem4.settext("租书"); 
 jmenu2.add(jmenuitem4); 
 
 jmenubar1.add(jmenu2); 
 
 jmenu1.settext("还书"); 
 
 jmenuitem3.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_h, java.awt.event.inputevent.ctrl_mask)); 
 jmenuitem3.settext("还书"); 
 jmenu1.add(jmenuitem3); 
 
 jmenubar1.add(jmenu1); 
 
 setjmenubar(jmenubar1); 
 
 javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); 
 getcontentpane().setlayout(layout); 
 layout.sethorizontalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addcomponent(jlabel1, 0, 0, short.max_value) 
  .addgroup(layout.createsequentialgroup() 
  .addgap(22, 22, 22) 
  .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) 
  .addcontainergap()) 
 ); 
 layout.setverticalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() 
  .addcontainergap() 
  .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) 
  .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) 
  .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 411, javax.swing.grouplayout.preferred_size)) 
 ); 
 
 pack(); 
 }// </editor-fold>    
 
 private void jmenuitem1actionperformed(java.awt.event.actionevent evt) {      
 // 类型管理 
  new typeform().setvisible(true); 
 }      
 
 private void jmenuitem2actionperformed(java.awt.event.actionevent evt) {      
 // 图书管理 
 new bookform().setvisible(true); 
 }      
 
 /** 
 * @param args the command line arguments 
 */ 
 public static void main(string args[]) { 
 java.awt.eventqueue.invokelater(new runnable() { 
  public void run() { 
  new mainform().setvisible(true); 
  } 
 }); 
 } 
 
 // variables declaration - do not modify   
 private javax.swing.jlabel jlabel1; 
 private javax.swing.jlabel jlabel2; 
 private javax.swing.jmenu jmenu1; 
 private javax.swing.jmenu jmenu2; 
 private javax.swing.jmenubar jmenubar1; 
 private javax.swing.jmenuitem jmenuitem1; 
 private javax.swing.jmenuitem jmenuitem2; 
 private javax.swing.jmenuitem jmenuitem3; 
 private javax.swing.jmenuitem jmenuitem4; 
 private javax.swing.jpanel jpanel1; 
 private javax.swing.jmenu 配置管理; 
 // end of variables declaration   
 
} 

图书管理系统java代码实现

bookform.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
/* 
 * bookform.java 
 * 
 * created on 2013-2-28, 8:23:01 
 */ 
 
package com.liu.view; 
 
import com.liu.dao.bookdao; 
import com.liu.dao.typedao; 
import com.liu.po.bookbean; 
import com.liu.po.typebean; 
import java.util.map; 
import java.util.set; 
import java.util.vector; 
import javax.swing.defaultcomboboxmodel; 
import javax.swing.joptionpane; 
import javax.swing.table.defaulttablemodel; 
 
/** 
 * 
 * @author asus 
 */ 
public class bookform extends javax.swing.jframe { 
 
 
 /** creates new form bookform */ 
 private map<integer,bookbean> map; 
 private map<integer,typebean> map1; 
 private bookdao bookdao; 
 private typedao typedao; 
 
 public bookform() { 
 initcomponents(); 
 bookdao = new bookdao(); 
 typedao = new typedao(); 
 map = bookdao.readbook(); 
 map1 = typedao.readtype(); 
 inittype(); 
 initdata(); 
 } 
 
 /** this method is called from within the constructor to 
 * initialize the form. 
 * warning: do not modify this code. the content of this method is 
 * always regenerated by the form editor. 
 */ 
 @suppresswarnings("unchecked") 
 // <editor-fold defaultstate="collapsed" desc="generated code">    
 private void initcomponents() { 
 
 jpanel1 = new javax.swing.jpanel(); 
 jscrollpane1 = new javax.swing.jscrollpane(); 
 booktable = new javax.swing.jtable(); 
 jpanel2 = new javax.swing.jpanel(); 
 bmemo = new javax.swing.jtextfield(); 
 jbutton1 = new javax.swing.jbutton(); 
 jbutton2 = new javax.swing.jbutton(); 
 jbutton3 = new javax.swing.jbutton(); 
 jbutton4 = new javax.swing.jbutton(); 
 jlabel1 = new javax.swing.jlabel(); 
 jlabel2 = new javax.swing.jlabel(); 
 jlabel3 = new javax.swing.jlabel(); 
 jlabel4 = new javax.swing.jlabel(); 
 bid = new javax.swing.jtextfield(); 
 bname = new javax.swing.jtextfield(); 
 btype = new javax.swing.jcombobox(); 
 jlabel5 = new javax.swing.jlabel(); 
 bmoney = new javax.swing.jtextfield(); 
 
 settitle("图书管理"); 
 setbounds(new java.awt.rectangle(100, 50, 0, 0)); 
 seticonimage(new javax.swing.imageicon(getclass().getresource("/com/liu/resouce/logo.jpg")).getimage()); 
 
 booktable.setfont(new java.awt.font("宋体", 0, 18)); // noi18n 
 booktable.setmodel(new javax.swing.table.defaulttablemodel( 
  new object [][] { 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null}, 
  {null, null, null, null, null} 
  }, 
  new string [] { 
  "序号", "图书名称", "图书类型", "租金", "备注" 
  } 
 ) { 
  class[] types = new class [] { 
  java.lang.integer.class, java.lang.string.class, java.lang.string.class, java.lang.string.class, java.lang.string.class 
  }; 
  boolean[] canedit = new boolean [] { 
  false, false, false, false, false 
  }; 
 
  public class getcolumnclass(int columnindex) { 
  return types [columnindex]; 
  } 
 
  public boolean iscelleditable(int rowindex, int columnindex) { 
  return canedit [columnindex]; 
  } 
 }); 
 booktable.addmouselistener(new java.awt.event.mouseadapter() { 
  public void mouseclicked(java.awt.event.mouseevent evt) { 
  booktablemouseclicked(evt); 
  } 
 }); 
 jscrollpane1.setviewportview(booktable); 
 
 jpanel2.setborder(javax.swing.borderfactory.createtitledborder(null, "详细信息", javax.swing.border.titledborder.default_justification, javax.swing.border.titledborder.default_position, new java.awt.font("宋体", 0, 18))); // noi18n 
 
 bmemo.setfont(new java.awt.font("宋体", 0, 18)); 
 
 jbutton1.settext("新增"); 
 jbutton1.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton1actionperformed(evt); 
  } 
 }); 
 
 jbutton2.settext("保存"); 
 jbutton2.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton2actionperformed(evt); 
  } 
 }); 
 
 jbutton3.settext("更新"); 
 jbutton3.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton3actionperformed(evt); 
  } 
 }); 
 
 jbutton4.settext("删除"); 
 jbutton4.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton4actionperformed(evt); 
  } 
 }); 
 
 jlabel1.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel1.settext("序号:"); 
 
 jlabel2.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel2.settext("名称:"); 
 
 jlabel3.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel3.settext("类型:"); 
 
 jlabel4.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel4.settext("备注:"); 
 
 bid.setfont(new java.awt.font("宋体", 0, 18)); 
 
 bname.setfont(new java.awt.font("宋体", 0, 18)); 
 
 btype.setfont(new java.awt.font("宋体", 0, 18)); 
 btype.setmodel(new javax.swing.defaultcomboboxmodel(new string[] { "文学类", "教育类", "科技类", "文艺类" })); 
 btype.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  btypeactionperformed(evt); 
  } 
 }); 
 
 jlabel5.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel5.settext("租金:"); 
 
 javax.swing.grouplayout jpanel2layout = new javax.swing.grouplayout(jpanel2); 
 jpanel2.setlayout(jpanel2layout); 
 jpanel2layout.sethorizontalgroup( 
  jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel2layout.createsequentialgroup() 
  .addgap(33, 33, 33) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
    .addcomponent(jlabel2, javax.swing.grouplayout.default_size, 83, short.max_value) 
    .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) 
    .addcomponent(jlabel3) 
    .addcomponent(jlabel5) 
    .addcomponent(jlabel4))) 
   .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addcomponent(jlabel1) 
   .addgap(33, 33, 33))) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
   .addcomponent(bmemo, javax.swing.grouplayout.default_size, 375, short.max_value) 
   .addcomponent(bid, javax.swing.grouplayout.default_size, 375, short.max_value) 
   .addcomponent(bname, javax.swing.grouplayout.default_size, 375, short.max_value) 
   .addcomponent(bmoney, javax.swing.grouplayout.default_size, 375, short.max_value) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 73, javax.swing.grouplayout.preferred_size) 
   .addgap(28, 28, 28) 
   .addcomponent(jbutton2, javax.swing.grouplayout.preferred_size, 69, javax.swing.grouplayout.preferred_size) 
   .addgap(30, 30, 30) 
   .addcomponent(jbutton3, javax.swing.grouplayout.preferred_size, 73, javax.swing.grouplayout.preferred_size) 
   .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 28, short.max_value) 
   .addcomponent(jbutton4, javax.swing.grouplayout.preferred_size, 74, javax.swing.grouplayout.preferred_size)) 
   .addcomponent(btype, 0, 375, short.max_value)) 
  .addgap(65, 65, 65)) 
 ); 
 jpanel2layout.setverticalgroup( 
  jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(javax.swing.grouplayout.alignment.trailing, jpanel2layout.createsequentialgroup() 
  .addcontainergap() 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 25, javax.swing.grouplayout.preferred_size) 
   .addcomponent(bid, javax.swing.grouplayout.preferred_size, 37, javax.swing.grouplayout.preferred_size)) 
  .addgap(13, 13, 13) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel2) 
   .addcomponent(bname, javax.swing.grouplayout.preferred_size, 37, javax.swing.grouplayout.preferred_size)) 
  .addgap(18, 18, 18) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel3) 
   .addcomponent(btype, javax.swing.grouplayout.preferred_size, 30, javax.swing.grouplayout.preferred_size)) 
  .addgap(26, 26, 26) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel5) 
   .addcomponent(bmoney, javax.swing.grouplayout.preferred_size, 35, javax.swing.grouplayout.preferred_size)) 
  .addgap(32, 32, 32) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addcomponent(bmemo, javax.swing.grouplayout.preferred_size, 58, javax.swing.grouplayout.preferred_size) 
   .addgap(64, 64, 64) 
   .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
    .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size) 
    .addcomponent(jbutton2, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size) 
    .addcomponent(jbutton3, javax.swing.grouplayout.preferred_size, 31, javax.swing.grouplayout.preferred_size) 
    .addcomponent(jbutton4, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size))) 
   .addcomponent(jlabel4)) 
  .addgap(22, 22, 22)) 
 ); 
 
 javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); 
 jpanel1.setlayout(jpanel1layout); 
 jpanel1layout.sethorizontalgroup( 
  jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addcomponent(jscrollpane1, javax.swing.grouplayout.alignment.trailing, javax.swing.grouplayout.default_size, 795, short.max_value) 
  .addgroup(jpanel1layout.createsequentialgroup() 
  .addgap(50, 50, 50) 
  .addcomponent(jpanel2, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) 
  .addcontainergap(173, short.max_value)) 
 ); 
 jpanel1layout.setverticalgroup( 
  jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel1layout.createsequentialgroup() 
  .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 192, javax.swing.grouplayout.preferred_size) 
  .addgap(10, 10, 10) 
  .addcomponent(jpanel2, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)) 
 ); 
 
 javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); 
 getcontentpane().setlayout(layout); 
 layout.sethorizontalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) 
 ); 
 layout.setverticalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addcomponent(jpanel1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) 
 ); 
 
 pack(); 
 }// </editor-fold>    
 //表格数据的初始化 
 public void initdata(){ 
 //加载数据 
 defaulttablemodel dtm = (defaulttablemodel)booktable.getmodel(); 
 //清空表 
 while(dtm.getrowcount()>0){ 
  dtm.removerow(0); 
 } 
 //加载数据 
 set<integer>set = map.keyset(); 
 for(integer i:set){ 
  bookbean bean = map.get(i); 
  vector v = new vector(); 
  v.add(bean.getid()); 
  v.add(bean.getbookname()); 
  v.add(bean.getbooktype()); 
  v.add(bean.getmoney()); 
  v.add(bean.getmemo()); 
  dtm.addrow(v); 
 } 
 } 
 //获取类别管理的所有类别 
 public void inittype(){ 
 set<integer> set = map1.keyset(); 
 defaultcomboboxmodel dcm = (defaultcomboboxmodel)btype.getmodel(); 
 dcm.removeallelements(); 
 for(integer i:set){ 
  typebean bean = map1.get(i); 
  dcm.addelement(bean.gettypename()); 
 } 
 
 } 
 private void jbutton2actionperformed(java.awt.event.actionevent evt) {      
 // 保存功能 
  //先保存文本框里的值 
 string id = bid.gettext(); 
 string bookname = bname.gettext(); 
 string booktype = (string) btype.getselecteditem(); 
 string memo = bmemo.gettext(); 
 string money = bmoney.gettext(); 
 
 //封装成对象 
 bookbean bean = new bookbean(); 
 
 bean.setid(integer.parseint(id)); 
 bean.setbookname(bookname); 
 bean.setbooktype(booktype); 
 bean.setmemo(memo); 
 bean.setmoney(money); 
 
 //将bean放到map里面 
  // map<integer,typebean>map = new hashmap<integer,typebean>(); 
 map.put(integer.parseint(id), bean); 
 //将map放到文件里面 
 bookdao.writebook(map); 
 //刷新table 
 initdata(); 
 }      
 
 private void jbutton1actionperformed(java.awt.event.actionevent evt) {      
  // 新增时,将文本框里的信息进行清空,并将序列号置为可编辑。 
 bid.setenabled(true); 
 
 bid.settext(""); 
 bname.settext(""); 
 btype.setselecteditem(""); 
 bmemo.settext(""); 
 bmoney.settext(""); 
 
 }      
 
 private void booktablemouseclicked(java.awt.event.mouseevent evt) {      
 //获取选中行号及序列号 
 int currentrow = booktable.getselectedrow(); 
 //bookbean bean = map.get( currentrow); 
  // 将选中的行,显示到信息栏中 
 bid.settext((integer) booktable.getvalueat(currentrow, 0)+""); 
 bname.settext((string) booktable.getvalueat(currentrow, 1)); 
 btype.setselecteditem((string) booktable.getvalueat(currentrow, 2)); 
 bmoney.settext((string) booktable.getvalueat(currentrow, 3)); 
 bmemo.settext((string) booktable.getvalueat(currentrow, 4)); 
 
 //bmemo.settext(bean.getmemo()); 
 
 bid.setenabled(false); //序号框不可编辑 
 }     
 
 private void jbutton4actionperformed(java.awt.event.actionevent evt) {      
  // 删除操作 
 //获取选中行号及序列号 
 int currentrow = booktable.getselectedrow(); 
 int id = (integer)booktable.getvalueat(currentrow, 0); 
 map.remove(id); 
 bookdao.writebook(map); 
 joptionpane.showmessagedialog(this,"类别删除成功"); 
 initdata(); 
 }      
 
 private void btypeactionperformed(java.awt.event.actionevent evt) {     
 // todo add your handling code here: 
}     
 
 private void jbutton3actionperformed(java.awt.event.actionevent evt) {      
 // 更新操作 
 //先保存文本框里的值 
 int currentrow = booktable.getselectedrow(); 
 int id = (integer) booktable.getvalueat(currentrow, 0); 
 
 string bookname = bname.gettext(); 
 string booktype = (string) btype.getselecteditem(); 
 string memo = bmemo.gettext(); 
 string money = bmoney.gettext(); 
 //封装成对象 
 bookbean bean = new bookbean(); 
 bean.setid(id); 
 bean.setbookname(bookname); 
 bean.setbooktype(booktype); 
 bean.setmemo(memo); 
 bean.setmoney(money); 
 
 //将bean放到map里面 
  // map<integer,typebean>map = new hashmap<integer,typebean>(); 
 map.put(id, bean); 
 //将map放到文件里面 
 bookdao.writebook(map); 
 //刷新table 
 joptionpane.showmessagedialog(this,"类别更新成功"); 
 initdata(); 
 }      
 
 /** 
 * @param args the command line arguments 
 */ 
 public static void main(string args[]) { 
 java.awt.eventqueue.invokelater(new runnable() { 
  public void run() { 
  new bookform().setvisible(true); 
  } 
 }); 
 } 
 
 // variables declaration - do not modify   
 private javax.swing.jtextfield bid; 
 private javax.swing.jtextfield bmemo; 
 private javax.swing.jtextfield bmoney; 
 private javax.swing.jtextfield bname; 
 private javax.swing.jtable booktable; 
 private javax.swing.jcombobox btype; 
 private javax.swing.jbutton jbutton1; 
 private javax.swing.jbutton jbutton2; 
 private javax.swing.jbutton jbutton3; 
 private javax.swing.jbutton jbutton4; 
 private javax.swing.jlabel jlabel1; 
 private javax.swing.jlabel jlabel2; 
 private javax.swing.jlabel jlabel3; 
 private javax.swing.jlabel jlabel4; 
 private javax.swing.jlabel jlabel5; 
 private javax.swing.jpanel jpanel1; 
 private javax.swing.jpanel jpanel2; 
 private javax.swing.jscrollpane jscrollpane1; 
 // end of variables declaration   
 
} 

图书管理系统java代码实现

typeform.java

/* 
 * to change this template, choose tools | templates 
 * and open the template in the editor. 
 */ 
 
/* 
 * typeform.java 
 * 
 * created on 2013-2-26, 19:07:51 
 */ 
 
package com.liu.view; 
import com.liu.dao.typedao; 
import com.liu.po.typebean; 
import java.util.hashmap; 
import java.util.map; 
import java.util.set; 
import java.util.vector; 
import javax.swing.joptionpane; 
import javax.swing.table.defaulttablemodel; 
 
/** 
 * 
 * @author asus 
 */ 
public class typeform extends javax.swing.jframe { 
 
 private typedao typedao; 
 private map<integer,typebean> map; 
 /** creates new form typeform */ 
 public typeform() { 
 initcomponents(); 
 typedao = new typedao(); 
 map = typedao.readtype(); 
 initdata(); 
 } 
 
 /** this method is called from within the constructor to 
 * initialize the form. 
 * warning: do not modify this code. the content of this method is 
 * always regenerated by the form editor. 
 */ 
 @suppresswarnings("unchecked") 
 // <editor-fold defaultstate="collapsed" desc="generated code">    
 private void initcomponents() { 
 
 jpanel1 = new javax.swing.jpanel(); 
 jscrollpane1 = new javax.swing.jscrollpane(); 
 typetable = new javax.swing.jtable(); 
 jpanel2 = new javax.swing.jpanel(); 
 jlabel1 = new javax.swing.jlabel(); 
 jlabel2 = new javax.swing.jlabel(); 
 jlabel3 = new javax.swing.jlabel(); 
 tid = new javax.swing.jtextfield(); 
 tname = new javax.swing.jtextfield(); 
 jscrollpane2 = new javax.swing.jscrollpane(); 
 tmemo = new javax.swing.jtextarea(); 
 jbutton1 = new javax.swing.jbutton(); 
 jbutton3 = new javax.swing.jbutton(); 
 jbutton4 = new javax.swing.jbutton(); 
 jbutton5 = new javax.swing.jbutton(); 
 
 settitle("类型管理"); 
 setbounds(new java.awt.rectangle(100, 50, 0, 0)); 
 seticonimage(new javax.swing.imageicon(getclass().getresource("/com/liu/resouce/logo.jpg")).getimage()); 
 
 typetable.setfont(new java.awt.font("宋体", 0, 18)); 
 typetable.setmodel(new javax.swing.table.defaulttablemodel( 
  new object [][] { 
  {null, null, null}, 
  {null, null, null}, 
  {null, null, null}, 
  {null, null, null} 
  }, 
  new string [] { 
  "序号", "类别名称", "备注" 
  } 
 ) { 
  class[] types = new class [] { 
  java.lang.integer.class, java.lang.string.class, java.lang.string.class 
  }; 
  boolean[] canedit = new boolean [] { 
  false, false, false 
  }; 
 
  public class getcolumnclass(int columnindex) { 
  return types [columnindex]; 
  } 
 
  public boolean iscelleditable(int rowindex, int columnindex) { 
  return canedit [columnindex]; 
  } 
 }); 
 typetable.setcolumnselectionallowed(true); 
 typetable.addmouselistener(new java.awt.event.mouseadapter() { 
  public void mouseclicked(java.awt.event.mouseevent evt) { 
  typetablemouseclicked(evt); 
  } 
 }); 
 typetable.addcontainerlistener(new java.awt.event.containeradapter() { 
  public void componentadded(java.awt.event.containerevent evt) { 
  typetablecomponentadded(evt); 
  } 
 }); 
 jscrollpane1.setviewportview(typetable); 
 typetable.getcolumnmodel().getselectionmodel().setselectionmode(javax.swing.listselectionmodel.single_interval_selection); 
 typetable.getcolumnmodel().getcolumn(0).setresizable(false); 
 typetable.getcolumnmodel().getcolumn(2).setresizable(false); 
 
 jpanel2.setborder(javax.swing.borderfactory.createtitledborder(null, "类别信息", javax.swing.border.titledborder.default_justification, javax.swing.border.titledborder.default_position, new java.awt.font("宋体", 0, 18))); // noi18n 
 
 jlabel1.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel1.settext("序号:"); 
 
 jlabel2.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel2.settext("类别名称:"); 
 
 jlabel3.setfont(new java.awt.font("宋体", 0, 18)); 
 jlabel3.settext("备注:"); 
 
 tid.setfont(new java.awt.font("宋体", 0, 18)); 
 tid.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  tidactionperformed(evt); 
  } 
 }); 
 
 tname.setfont(new java.awt.font("宋体", 0, 18)); 
 
 tmemo.setcolumns(20); 
 tmemo.setfont(new java.awt.font("monospaced", 0, 18)); 
 tmemo.setrows(5); 
 jscrollpane2.setviewportview(tmemo); 
 
 jbutton1.settext("保存"); 
 jbutton1.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton1actionperformed(evt); 
  } 
 }); 
 
 jbutton3.settext("更新"); 
 jbutton3.addmouselistener(new java.awt.event.mouseadapter() { 
  public void mouseclicked(java.awt.event.mouseevent evt) { 
  jbutton3mouseclicked(evt); 
  } 
 }); 
 jbutton3.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton3actionperformed(evt); 
  } 
 }); 
 
 jbutton4.settext("删除"); 
 jbutton4.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton4actionperformed(evt); 
  } 
 }); 
 
 jbutton5.settext("新增"); 
 jbutton5.addactionlistener(new java.awt.event.actionlistener() { 
  public void actionperformed(java.awt.event.actionevent evt) { 
  jbutton5actionperformed(evt); 
  } 
 }); 
 
 javax.swing.grouplayout jpanel2layout = new javax.swing.grouplayout(jpanel2); 
 jpanel2.setlayout(jpanel2layout); 
 jpanel2layout.sethorizontalgroup( 
  jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel2layout.createsequentialgroup() 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addgap(39, 39, 39) 
   .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
    .addgroup(jpanel2layout.createsequentialgroup() 
    .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) 
    .addcomponent(jlabel2, javax.swing.grouplayout.preferred_size, 90, javax.swing.grouplayout.preferred_size) 
    .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) 
    .addcomponent(tname, javax.swing.grouplayout.default_size, 341, short.max_value)) 
    .addgroup(jpanel2layout.createsequentialgroup() 
    .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) 
    .addcomponent(jlabel3) 
    .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) 
    .addcomponent(jscrollpane2, javax.swing.grouplayout.preferred_size, 383, javax.swing.grouplayout.preferred_size)) 
    .addgroup(jpanel2layout.createsequentialgroup() 
    .addcomponent(jlabel1) 
    .addpreferredgap(javax.swing.layoutstyle.componentplacement.related) 
    .addcomponent(tid, javax.swing.grouplayout.default_size, 383, short.max_value)))) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addgap(70, 70, 70) 
   .addcomponent(jbutton5, javax.swing.grouplayout.preferred_size, 69, javax.swing.grouplayout.preferred_size) 
   .addgap(41, 41, 41) 
   .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 68, javax.swing.grouplayout.preferred_size) 
   .addgap(37, 37, 37) 
   .addcomponent(jbutton3, javax.swing.grouplayout.preferred_size, 68, javax.swing.grouplayout.preferred_size) 
   .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 40, short.max_value) 
   .addcomponent(jbutton4, javax.swing.grouplayout.preferred_size, 67, javax.swing.grouplayout.preferred_size) 
   .addgap(20, 20, 20))) 
  .addgap(83, 83, 83)) 
 ); 
 jpanel2layout.setverticalgroup( 
  jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel2layout.createsequentialgroup() 
  .addgap(31, 31, 31) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 21, javax.swing.grouplayout.preferred_size) 
   .addcomponent(tid, javax.swing.grouplayout.preferred_size, 34, javax.swing.grouplayout.preferred_size)) 
  .addgap(27, 27, 27) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jlabel2, javax.swing.grouplayout.preferred_size, 21, javax.swing.grouplayout.preferred_size) 
   .addcomponent(tname, javax.swing.grouplayout.preferred_size, 34, javax.swing.grouplayout.preferred_size)) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addgap(45, 45, 45) 
   .addcomponent(jlabel3)) 
   .addgroup(jpanel2layout.createsequentialgroup() 
   .addgap(24, 24, 24) 
   .addcomponent(jscrollpane2, javax.swing.grouplayout.preferred_size, 92, javax.swing.grouplayout.preferred_size))) 
  .addgap(35, 35, 35) 
  .addgroup(jpanel2layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) 
   .addcomponent(jbutton5, javax.swing.grouplayout.preferred_size, 34, javax.swing.grouplayout.preferred_size) 
   .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 33, javax.swing.grouplayout.preferred_size) 
   .addcomponent(jbutton3, javax.swing.grouplayout.preferred_size, 32, javax.swing.grouplayout.preferred_size) 
   .addcomponent(jbutton4, javax.swing.grouplayout.preferred_size, 33, javax.swing.grouplayout.preferred_size)) 
  .addgap(18, 18, 18)) 
 ); 
 
 javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1); 
 jpanel1.setlayout(jpanel1layout); 
 jpanel1layout.sethorizontalgroup( 
  jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel1layout.createsequentialgroup() 
  .addgap(66, 66, 66) 
  .addcomponent(jpanel2, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) 
  .addcontainergap(55, short.max_value)) 
  .addcomponent(jscrollpane1, javax.swing.grouplayout.default_size, 696, short.max_value) 
 ); 
 jpanel1layout.setverticalgroup( 
  jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(jpanel1layout.createsequentialgroup() 
  .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 177, javax.swing.grouplayout.preferred_size) 
  .addgap(18, 18, 18) 
  .addcomponent(jpanel2, javax.swing.grouplayout.default_size, 361, short.max_value) 
  .addcontainergap()) 
 ); 
 
 javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); 
 getcontentpane().setlayout(layout); 
 layout.sethorizontalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addcomponent(jpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) 
 ); 
 layout.setverticalgroup( 
  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) 
  .addgroup(layout.createsequentialgroup() 
  .addcomponent(jpanel1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) 
  .addcontainergap(javax.swing.grouplayout.default_size, short.max_value)) 
 ); 
 
 pack(); 
 }// </editor-fold>    
 
 //表格数据的初始化 
 public void initdata(){ 
 //加载数据 
 defaulttablemodel dtm = (defaulttablemodel)typetable.getmodel(); 
 //清空表 
 while(dtm.getrowcount()>0){ 
  dtm.removerow(0); 
 } 
 //加载数据 
 set<integer>set = map.keyset(); 
 for(integer i:set){ 
  typebean bean = map.get(i); 
  vector v = new vector(); 
  v.add(bean.getid()); 
  v.add(bean.gettypename()); 
  v.add(bean.getmemo()); 
  dtm.addrow(v); 
 } 
 } 
 
 private void tidactionperformed(java.awt.event.actionevent evt) {     
 // todo add your handling code here: 
}     
 
 private void jbutton1actionperformed(java.awt.event.actionevent evt) {      
 //保存类型操作 
 //先保存文本框里的值 
 string id = tid.gettext(); 
 string typename = tname.gettext(); 
 string memo = tmemo.gettext(); 
 //封装成对象 
 typebean bean = new typebean(); 
 bean.setid(integer.parseint(id)); 
 bean.setmemo(memo); 
 bean.settypename(typename); 
 //将bean放到map里面 
  // map<integer,typebean>map = new hashmap<integer,typebean>(); 
 map.put(integer.parseint(id), bean); 
 //将map放到文件里面 
 typedao.writetype(map); 
 //刷新table 
 initdata(); 
 }      
 
 private void typetablecomponentadded(java.awt.event.containerevent evt) {      
 // todo add your handling code here: 
}      
 
 private void jbutton4actionperformed(java.awt.event.actionevent evt) {      
 // 删除操作 
 //获取选中行号及序列号 
 int currentrow = typetable.getselectedrow(); 
 int id = (integer)typetable.getvalueat(currentrow, 0); 
 map.remove(id); 
 typedao.writetype(map); 
 joptionpane.showmessagedialog(this,"类别删除成功"); 
 initdata(); 
 }      
 
 private void jbutton3mouseclicked(java.awt.event.mouseevent evt) {     
 
 
 }     
 
 private void jbutton3actionperformed(java.awt.event.actionevent evt) {      
 // 更新操作 
 //先保存文本框里的值 
 int currentrow = typetable.getselectedrow(); 
 int id = (integer) typetable.getvalueat(currentrow, 0); 
  
 string typename = tname.gettext(); 
 string memo = tmemo.gettext(); 
 //封装成对象 
 typebean bean = new typebean(); 
 bean.setid(id); 
 bean.setmemo(memo); 
 bean.settypename(typename); 
 //将bean放到map里面 
  // map<integer,typebean>map = new hashmap<integer,typebean>(); 
 map.put(id, bean); 
 //将map放到文件里面 
 typedao.writetype(map); 
 //刷新table 
 joptionpane.showmessagedialog(this,"类别更新成功"); 
 initdata(); 
 }      
 
 private void typetablemouseclicked(java.awt.event.mouseevent evt) {      
  //获取选中行号及序列号 
 int currentrow = typetable.getselectedrow(); 
  // 将选中的行,显示到信息栏中 
 tid.settext((integer) typetable.getvalueat(currentrow, 0)+""); 
 tname.settext((string) typetable.getvalueat(currentrow, 1)); 
 tmemo.settext((string) typetable.getvalueat(currentrow, 2)); 
 
 tid.setenabled(false); //序号框不可编辑 
 
 }     
 
 private void jbutton5actionperformed(java.awt.event.actionevent evt) {      
 // 新增时,将文本框里的信息进行清空,并将序列号置为可编辑。 
 tid.setenabled(true); 
 tid.settext(""); 
 tname.settext(""); 
 tmemo.settext(""); 
 
 }      
 
 /** 
 * @param args the command line arguments 
 */ 
 public static void main(string args[]) { 
 java.awt.eventqueue.invokelater(new runnable() { 
  public void run() { 
  new typeform().setvisible(true); 
  } 
 }); 
 } 
 
 // variables declaration - do not modify   
 private javax.swing.jbutton jbutton1; 
 private javax.swing.jbutton jbutton3; 
 private javax.swing.jbutton jbutton4; 
 private javax.swing.jbutton jbutton5; 
 private javax.swing.jlabel jlabel1; 
 private javax.swing.jlabel jlabel2; 
 private javax.swing.jlabel jlabel3; 
 private javax.swing.jpanel jpanel1; 
 private javax.swing.jpanel jpanel2; 
 private javax.swing.jscrollpane jscrollpane1; 
 private javax.swing.jscrollpane jscrollpane2; 
 private javax.swing.jtextfield tid; 
 private javax.swing.jtextarea tmemo; 
 private javax.swing.jtextfield tname; 
 private javax.swing.jtable typetable; 
 // end of variables declaration   
 
} 

图书管理系统java代码实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: