图书管理系统

    xiaoxiao2021-03-25  125

    图书管理系统

    该图书管理系统是我研一时候的一个课程设计,当时也刚重新学完Java。你可能会“重新”是什么意思。唉,说来惭愧,大学时我先学了点Java的皮毛,就投奔搞Android去了,所以对Java的基础比较薄弱。而现在学完了,所以就想试着弄个小系统来练练手,所以参考了上一篇中国象棋的代码,就做了这个图书管理系统。 同样,这个系统采用的是客户端/服务端(C/S)的模式,相对于上一篇的中国象棋,该系统保留了Socket和多线程编程外,增加了JDBC编程,对数据库进行操作。数据库采用的是SqlServer 2005,建立了用户信息表以及图书信息表,两者是一对多的关系,下面下载的源码中已包含了建数据库和建表,以及插入用户数据以及图书数据。 此外,我还增加了手机移动端,毕竟之前玩过Android,就想做着玩玩。 该图书管理系统包含如下四个模块,分别是登录模块,查询模块,借书模块,还书模块。 用户验证登录:本系统含有三种用户级别,分别是学生、老师、管理员。学生和老师是一样的,只能查询和借还书,而管理员可以对用户和书本的数据执行增删改查。 图书的查询检索:可进行模糊查询,包括按书名查询,按作者查询,按出版社查询,按索引号查询。 图书的借阅和还书:借书还书记录的登记和清除。

    (1)系统整体框架

    整个系统只能在局域网里运行,而且这里并没有考虑多线程下的并发问题,对于多个用户同时访问同一数据资源并不做考虑。 服务端和客户端的交互过程(ServerSocket、Socket的交互过程) 电脑客户端 手机客户端

    (2)服务器端

    服务器端参照一篇中国象棋的服务器,我只是该了一下业务逻辑的代码。

    服务端UI代码

    package Library_Server; public class Library_ServerUI extends JFrame implements ActionListener{ JLabel jlPort=new JLabel("端 口 号");//创建提示输入端口号标签 JTextField jtfPort=new JTextField("9999");//用于输入端口号的文本框 JButton jbStart=new JButton("启动");//创建"启动"按钮 JButton jbStop=new JButton("关闭");//创建"关闭"按钮 JPanel jps=new JPanel();//创建一个JPanel对象 JList jlUserOnline=new JList();//创建用于显示当前用户的JList JScrollPane jspx=new JScrollPane(jlUserOnline);//将显示当前用户的JList放在JScrollPane中 ServerSocket socket;//声明ServerSocket引用 ServerThread serverthread;//声明ServerThread引用 JSplitPane jspz=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jspx,jps);//创建JSplitPane对象 Vector onlineList=new Vector(); public Library_ServerUI() { initialComponent(); addListener(); initialFrame(); } public static void main(String[] args) { new Library_ServerUI(); } public void initialComponent(){ jps.setLayout(null);//设为空布局 jlPort.setBounds(20,20,50,20); jps.add(jlPort);//添加用于提示输入端口号的标签 this.jtfPort.setBounds(85,20,60,20); jps.add(this.jtfPort);//添加用于输入端口号的文本框 this.jbStart.setBounds(18,50,60,20); jps.add(this.jbStart);//添加"开始"按钮 this.jbStop.setBounds(85,50,60,20); jps.add(this.jbStop);//添加"关闭"按钮 this.jbStop.setEnabled(false);//将"关闭"按钮设为不可用 } public void addListener(){ this.jbStart.addActionListener(this);//为"开始"按钮注册事件监听器 this.jbStop.addActionListener(this);//为"关闭"按钮注册事件监听器 } public void initialFrame(){ this.setTitle("图书管理系统--服务器端");//设置窗体标题 Image image=new ImageIcon("./image/library_ico.jpg").getImage(); this.setIconImage(image); this.add(jspz);//将JSplitPane添加到窗体中 jspz.setDividerLocation(250); jspz.setDividerSize(4);//设置分割线的位置和宽度 Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕长宽 this.setBounds(scrSize.width/3,scrSize.height/3,420,320); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);//设置可见性 this.setResizable(false); this.addWindowListener(mywindowadapter); } public WindowAdapter mywindowadapter=new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); }; }; @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==this.jbStart) {//当单击"启动"按钮时 this.jbStart_event(); } else if(e.getSource()==this.jbStop) {//单击"关闭"按钮后 this.jbStop_event(); } } public void jbStart_event(){ //单击"启动"按钮的业务处理代码 int port=0; try{ //获得用户输入的端口号,并转化为整型 port=Integer.parseInt(this.jtfPort.getText().trim()); }catch(Exception ee){//端口号不是整数,给出提示信息 JOptionPane.showMessageDialog(this,"端口号只能是整数","错误",JOptionPane.ERROR_MESSAGE); return; }if(port>65535||port<0){//断口号不合法,给出提示信息 JOptionPane.showMessageDialog(this,"端口号只能是0-65535的整数","错误",JOptionPane.ERROR_MESSAGE); return; } try{ this.jbStart.setEnabled(false);//将开始按钮设为不可用 this.jtfPort.setEnabled(false);//将用于输入端口号的文本框设为不可用 this.jbStop.setEnabled(true);//将停止按钮设为可用 socket=new ServerSocket(port);//创建ServerSocket对象 serverthread=new ServerThread(this,socket);//创建服务器线程 serverthread.start();//启动服务器线程 }catch(Exception ee){ //给出服务器启动失败的提示信息 JOptionPane.showMessageDialog(this,"服务器启动失败","错误",JOptionPane.ERROR_MESSAGE); this.jbStart.setEnabled(true);//将开始按钮设为可用 this.jtfPort.setEnabled(true);//将用于输入端口号的文本框设为可用 this.jbStop.setEnabled(false);//将停止按钮设为不可用 } } public void jbStop_event() { //单击"关闭"按钮的业务处理代码 try{ socket.close(); serverthread=null; this.jbStart.setEnabled(true);//将开始按钮设为可用 this.jtfPort.setEnabled(true);//将用于输入端口号的文本框设为可用 this.jbStop.setEnabled(false);//将停止按钮设为不可用 }catch(Exception ee){ ee.printStackTrace(); } } public void refreshList(){ this.jlUserOnline.setListData(onlineList); } }

    监听用户连接

    package Library_Server; public class ServerThread extends Thread{ Library_ServerUI father; ServerSocket ss;//声明ServerSocket的引用 ObjectOutputStream Write_out; ObjectInputStream Read_in; IO_in IOin=new IO_in(); IO_out IOout=new IO_out(); String socket_name; public ServerThread( Library_ServerUI father,ServerSocket ss) { this.father=father; this.ss=ss; } @SuppressWarnings("unchecked") @Override public void run(){ super.run(); while(true){ try{ Socket socket=ss.accept();//等待客户端连接 if(Authentication(socket)){ this.father.onlineList.addElement(socket_name); this.father.refreshList(); new ServerAgentThread(father,socket,socket_name).start(); } }catch(Exception e){ e.printStackTrace(); } } } public boolean Authentication(Socket socket) throws Exception{ Read_in=new ObjectInputStream(socket.getInputStream()); User user=(User)IOin.ReceiveMessage(Read_in); socket_name=user.getUserId(); String[] user_info=qurey_position(user.getUserId(),user.getPasswd());//得到数据库中的数据 if(user.getPosition().equals(user_info[3])){//接收到的职位与数据库中的职位对比 user.setIsuser(true); user.setUsername(user_info[1]); } Write_out=new ObjectOutputStream(socket.getOutputStream()); IOout.SendMessage(Write_out,user); return user.grtIsuser(); } public String[] qurey_position(String user,String passwd){ String[] user_info = new String[4]; Database database = null; String sql="select account,user_name,password,position from User_Info where account=? and password=?"; try{ database=new Database(); ResultSet rs=database.query_user(sql, user, passwd); if(rs.next()) { user_info[0]=rs.getString(1);//account user_info[1]=rs.getString(2);//user_name user_info[2]=rs.getString(3);//password user_info[3]=rs.getString(4);//position } }catch(Exception e){} finally { database.close(); } return user_info; } }

    服务端代理线程代码

    package Library_Server; public class ServerAgentThread extends Thread{ Library_ServerUI father; Socket socket;//声明Socket的引用 /* PrintWriter Write_out;//获取Socket的输出流,用来向客户端发送数据 BufferedReader Read_in;//获取Socket的输入流,用来接收从客户端发送过来的数据 */ ObjectOutputStream Write_out; ObjectInputStream Read_in; boolean flag=true;//控制线程的标志位 IO_in IOin=new IO_in(); IO_out IOout=new IO_out(); public ServerAgentThread(Library_ServerUI father,Socket sc,String socket_name) throws UnsupportedEncodingException, IOException { this.father=father; this.socket=sc; this.setName(socket_name); /* Write_out= new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream(),"utf-8")),true); Read_in= new BufferedReader(new InputStreamReader(this.socket.getInputStream(),"utf-8")); */ Read_in=new ObjectInputStream(socket.getInputStream()); Write_out=new ObjectOutputStream(this.socket.getOutputStream()); } @Override public void run() { super.run(); while (flag) { try { String sql = null; String msg=(String)IOin.ReceiveMessage(Read_in); if(msg.startsWith(Order_format.All_book)){//从数据库中返回所有书并打包发送 sql="select * from Book_Info"; Vector<Book_Info> vector= Search_Book(sql); IOout.SendMessage(Write_out,vector); }else if(msg.startsWith(Order_format.bookname)){//返回搜索的书 sql="select * from Book_Info where bookname like '"+msg.substring(Order_format.count2) +"%' "+"or bookname like '%"+msg.substring(Order_format.count2)+"%'"; Vector<Book_Info> vector= Search_Book(sql); IOout.SendMessage(Write_out,vector); }else if(msg.startsWith(Order_format.author)){ sql="select * from Book_Info where author like '"+msg.substring(Order_format.count3) +"%' "+"or author like '%"+msg.substring(Order_format.count3)+"%'"; Vector<Book_Info> vector= Search_Book(sql); IOout.SendMessage(Write_out,vector); }else if(msg.startsWith(Order_format.Callno)){ sql="select * from Book_Info where Callno like '"+msg.substring(Order_format.count5) +"%' "+"or Callno like '%"+msg.substring(Order_format.count5)+"%'"; Vector<Book_Info> vector= Search_Book(sql); IOout.SendMessage(Write_out,vector); }else if(msg.startsWith(Order_format.publishment)){ sql="select * from Book_Info where publishment like '"+msg.substring(Order_format.count4) +"%' "+"or publishment like '%"+msg.substring(Order_format.count4)+"%'"; Vector<Book_Info> vector= Search_Book(sql); IOout.SendMessage(Write_out,vector); }else if(msg.startsWith(Order_format.Borrow_Book)){ sql="select * from Record_Info where account=?"; String[] user_book=Get_Borrow_book(sql,this.getName());//得到借的5本书,不管有没有 Update_Borrow_Record(user_book,msg.substring(Order_format.count6),this.getName()); }else if(msg.startsWith(Order_format.User_book)){ sql="select * from Record_Info where account=?"; String[] user_book=Get_Borrow_book(sql,msg.substring(Order_format.count7));//得到借的5本书,不管有没有 GetBack_Book(user_book); }else if(msg.startsWith(Order_format.Return_Book)){ sql="select * from Record_Info where account=?"; String[] user_book=Get_Borrow_book(sql,this.getName());//得到借的5本书,不管有没有 Update_Return_Record(user_book,msg.substring(Order_format.count8),this.getName());//换的书变为null,并设置该书在架上 String[] user_book1=Get_Borrow_book(sql,this.getName());//得到更新后的借的5本书 GetBack_Book(user_book1); }else if(msg.startsWith(Order_format.Exit)){ this.father.onlineList.removeElement(this.getName()); this.father.refreshList(); socket.close(); flag=false; } } catch (ClassNotFoundException|IOException e){ e.printStackTrace(); } } } public Vector<Book_Info> Search_Book(String sql){ Database database = null; Book_Info book_info; Vector<Book_Info> vector=new Vector<Book_Info>(); try{ database=new Database(); ResultSet rs=database.query_Book(sql); while(rs.next()) { book_info=new Book_Info(); book_info.setBookname(rs.getString(1)); book_info.setAuthor(rs.getString(2)); book_info.setPublishment(rs.getString(3)); book_info.setState(rs.getString(4)); book_info.setCallno(rs.getString(5)); vector.addElement(book_info); } }catch(Exception e){} finally { database.close(); } return vector; } public String[] Get_Borrow_book(String sql,String loginID){ Database database = null; String[] user_book = new String[5]; try { database=new Database(); ResultSet rs=database.query_Bookcount(sql, loginID); while(rs.next()){ for(int i=0;i<user_book.length;i++) user_book[i]=rs.getString(i+2); } } catch (Exception e) {} finally { database.close(); } return user_book; } public void Update_Borrow_Record(String[] user_book,String bookname,String loginID){ Database database = null; String sql=null; boolean flag=false; if(user_book[0]==null){ sql="update Record_Info set Borrow_Book1='"+bookname+"' where account=?"; }else if(user_book[1]==null){ sql="update Record_Info set Borrow_Book2='"+bookname+"' where account=?"; }else if(user_book[2]==null){ sql="update Record_Info set Borrow_Book3='"+bookname+"' where account=?"; }else if(user_book[3]==null){ sql="update Record_Info set Borrow_Book4='"+bookname+"' where account=?"; }else if(user_book[4]==null){ sql="update Record_Info set Borrow_Book5='"+bookname+"' where account=?"; }else{ flag=true; } if(!flag){ try { database=new Database(); database.Update_Record(sql,loginID); } catch (Exception e) {} finally { database.close(); } sql="update Book_Info set state='已借出' where bookname=?"; try { database=new Database(); database.Update_Allbook(sql, bookname); } catch (SQLException e) {} finally { database.close(); } sql="select * from Book_Info"; try { IOout.SendMessage(Write_out,Search_Book(sql)); } catch (IOException e) {} }else{ try { IOout.SendMessage(Write_out,"<#All_out#>对不起,你的借阅数量已达上限!"); } catch (IOException e) {} } } public void Update_Return_Record(String[] user_book,String bookname,String loginID){ Database database = null; String sql=null; for(int i=0;i<user_book.length;i++){//防止user_book[0].equals(bookname)出错 if(user_book[i]==null) user_book[i]=""; } if(user_book[0].equals(bookname)){ sql="update Record_Info set Borrow_Book1=null where account=?"; }else if(user_book[1].equals(bookname)){ sql="update Record_Info set Borrow_Book2=null where account=?"; }else if(user_book[2].equals(bookname)){ sql="update Record_Info set Borrow_Book3=null where account=?"; }else if(user_book[3].equals(bookname)){ sql="update Record_Info set Borrow_Book4=null where account=?"; }else if(user_book[4].equals(bookname)){ sql="update Record_Info set Borrow_Book5=null where account=?"; } try { database=new Database(); database.Update_Record(sql,loginID); } catch (Exception e) {} finally { database.close(); } sql="update Book_Info set state='在架上' where bookname=?"; try { database=new Database(); database.Update_Allbook(sql, bookname); } catch (SQLException e) {} finally { database.close(); } } public void GetBack_Book(String[] user_book){ String sql="select * from Book_Info where bookname like '"+user_book[0]+"' or bookname like '"+user_book[1] +"' or bookname like '"+user_book[2]+"' or bookname like '"+user_book[3]+"' or bookname like '"+user_book[4]+"'"; Vector<Book_Info> vector=Search_Book(sql); try { IOout.SendMessage(Write_out,vector); } catch (IOException e) { e.printStackTrace(); } } }

    (3)电脑客户端

    客户端就只贴电脑客户端的代码,至于手机客户端就不贴了,感兴趣的可以自行下载代码!

    用户登陆UI

    package Library_Client; public class Library_LoginUI extends JFrame implements ActionListener{ public static final String title="图书馆系统"; ImageIcon background=new ImageIcon("./image/background.jpg"); JPanel jps=new JPanel();//创建一个JPanel对象 JLabel Jl_background=new JLabel(background); JLabel Jl_title=new JLabel(title,JLabel.CENTER); JLabel Jl_IP=new JLabel("IP:",JLabel.RIGHT); JLabel Jl_stytle=new JLabel("类型:",JLabel.RIGHT); JLabel Jl_user=new JLabel("账号:",JLabel.RIGHT); JLabel Jl_password=new JLabel("密码:",JLabel.RIGHT); JTextField Jf_IP=new JTextField("127.0.0.1"); JTextField Jf_user=new JTextField("1534041005"); JComboBox<String> Jb_stytle=new JComboBox<String>(); JPasswordField jpassword=new JPasswordField("1234567"); JButton Jlogin=new JButton(new ImageIcon("./image/login.jpg")); Socket socket;//声明Socket引用 ClientThread clientThread; ObjectOutputStream Write_out; ObjectInputStream Read_in; IO_in IOin=new IO_in(); IO_out IOout=new IO_out(); public Library_LoginUI() { initialComponent(); addListener(); initialFrame(); } public static void main(String[] args) { new Library_LoginUI(); } public void initialComponent(){ jps.setLayout(null);//设为空布局 Jl_title.setBounds(350, 100, 300, 50); Jl_title.setFont(new Font(title, 1,50)); Jl_title.setForeground(Color.GREEN); jps.add(Jl_title); Jl_IP.setBounds(250, 250, 110, 25); Jl_IP.setFont(new Font("IP:", 1,25)); Jl_IP.setForeground(Color.lightGray); jps.add(Jl_IP); Jl_stytle.setBounds(250, 300, 110, 25); Jl_stytle.setFont(new Font("类型:", 1,25)); Jl_stytle.setForeground(Color.lightGray); jps.add(Jl_stytle); Jl_user.setBounds(250, 350, 110, 25); Jl_user.setFont(new Font("账号:", 1,25)); Jl_user.setForeground(Color.lightGray); jps.add(Jl_user); Jl_password.setBounds(250, 400, 110, 25); Jl_password.setFont(new Font("密码:", 1,25)); Jl_password.setForeground(Color.lightGray); jps.add(Jl_password); Jf_IP.setBounds(370, 250, 200, 25); jps.add(Jf_IP); Jb_stytle.setBounds(370, 300, 200, 25); Vector<String> v=new Vector<String>(); v.add("学生"); v.add("管理员"); v.add("老师"); Jb_stytle.setModel(new DefaultComboBoxModel<String>(v)); jps.add(Jb_stytle); Jf_user.setBounds(370, 350, 200,25); jps.add(Jf_user); jpassword.setBounds(370, 400, 200,25); jps.add(jpassword); Jlogin.setBounds(620, 385, 80,40); jps.add(Jlogin); Jl_background.setBounds(0, 0, background.getIconWidth(),background.getIconHeight()); jps.add(Jl_background); } public void addListener(){ Jlogin.addActionListener(this); } public void initialFrame(){ this.setTitle("图书管理系统--客户端");//设置窗体标题 Image image=new ImageIcon("./image/library_ico.jpg").getImage(); this.setIconImage(image); Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕长宽 this.setBounds(scrSize.width/6,scrSize.height/6, background.getIconWidth(),background.getIconHeight()); this.add(jps); this.setVisible(true);//设置可见性 this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(mywindowadapter); } public WindowAdapter mywindowadapter=new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); // socket.close(); }; }; @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==this.Jlogin){ jbConnect_event(); } } public void jbConnect_event(){ String ip=Jf_IP.getText().trim(); String user=Jf_user.getText().trim(); String password=jpassword.getText().trim(); String position=(String)Jb_stytle.getSelectedItem(); if(!isIP(ip)){ JOptionPane.showMessageDialog(this,"IP地址错误!","错误",JOptionPane.ERROR_MESSAGE); return; } if(!(user.length()>0)||!(password.length()>0)) { JOptionPane.showMessageDialog(this,"用户名和密码不能为空!","错误",JOptionPane.ERROR_MESSAGE); return; } try{ socket=new Socket(ip,9999);//创建Socket对象 User user_info=Authentication(socket,user,password,position); if(user_info.grtIsuser()){ if(position.equals("管理员")){ }else{ new Library_ClientUI(socket,user_info.getUserId(),user_info.getUsername(),user_info.getPosition()); } dispose(); } }catch(Exception e){} } public boolean isIP(String addr) { if(addr.length() < 7 || addr.length() > 15 || "".equals(addr)) { return false; } /** * 判断IP格式和范围 */ String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"; Pattern pat = Pattern.compile(rexp); Matcher mat = pat.matcher(addr); boolean ipAddress = mat.find(); return ipAddress; } public User Authentication(Socket socket,String user,String password,String position) throws IOException, ClassNotFoundException{ User u=new User(); u.setUserId(user); u.setPasswd(password); u.setIsuser(false); u.setPosition(position); Write_out=new ObjectOutputStream(socket.getOutputStream()); IOout.SendMessage(Write_out, u); Read_in=new ObjectInputStream(socket.getInputStream()); User user1=(User)IOin.ReceiveMessage(Read_in); return user1; } }

    电脑客户端UI

    用户登陆后,点检索键会把所有图书的信息列出来,从中我们可以看到那些图书已经借出去了,哪些还没有借。对于已经借出去的图书,你是不能选中的,而没借出去的可以双击选中。 package Library_Client; public class Library_ClientUI extends JFrame implements ActionListener,MouseListener{ Socket socket; String user_ID,position,user_name; ObjectOutputStream Write_out; ObjectInputStream Read_in; IO_in IOin=new IO_in(); IO_out IOout=new IO_out(); JPanel title_Jpanel=new JPanel(); JLabel title_bg; JLabel title; JLabel Welcome; ImagePanel Menu_Jpanel; JLabel Menu_title; JLabel Menu1,Menu2,Menu3,Menu4,Menu5,Menu6; Image Menu_bg; JPanel Main_Jpanel=new JPanel(); JLabel Spacer_bar; JPanel inter_Jpanel=new JPanel(); JPanel Search_Jpanel=new JPanel(); JComboBox<String> Search_Key; String[] key={"关键词","索书号","书名","作者名","出版社"}; JTextField key_name; JButton Search_Button; Show_BookList List_Jpanel; JPanel title_name=new JPanel(); JLabel Sel_book,Book_name,Author,Status,Callno; public Library_ClientUI(Socket socket,String user_ID,String user_name,String position) throws IOException { this.socket=socket; this.user_ID=user_ID; this.position=position; this.user_name=user_name; Write_out=new ObjectOutputStream(this.socket.getOutputStream()); Read_in=new ObjectInputStream(this.socket.getInputStream()); initialComponent(); initialFrame(); } public Library_ClientUI(){ initialComponent(); initialFrame(); } /* public static void main(String[] args) { new Library_ClientUI(); }*/ private void initialComponent(){ title_Jpanel.setLayout(new BorderLayout()); title=new JLabel("暨南大学图书馆",JLabel.CENTER); title.setBounds(300, 10,400, 80); title.setFont(new Font("暨南大学图书馆", 1,50)); title.setForeground(Color.black); title_Jpanel.add(title,BorderLayout.NORTH); Welcome=new JLabel("欢迎!"+user_name); Welcome.setFont(new Font("欢迎!"+user_name, 1,15)); Welcome.setForeground(Color.black); title_Jpanel.add(Welcome,BorderLayout.SOUTH); title_bg=new JLabel(new ImageIcon("./image/title_bg.jpg"),JLabel.CENTER); title_bg.setBounds(0, 0, 1000, 100); title_Jpanel.add(title_bg,BorderLayout.NORTH); try { Menu_bg=ImageIO.read(new File("./image/Menu_bg.jpg")); } catch (Exception e) {} Menu_Jpanel=new ImagePanel(Menu_bg); Menu_Jpanel.setLayout(new GridLayout(7,1)); Cursor myCursor=new Cursor(Cursor.HAND_CURSOR); Menu_title=new JLabel(new ImageIcon("./image/mylibrary.jpg"),JLabel.CENTER); Menu_Jpanel.add(Menu_title); Menu1=new JLabel("借阅图书",new ImageIcon("image/Borrow_book.jpg"),JLabel.CENTER); Menu1.setCursor(myCursor); Menu1.setFont(new Font("借阅图书", 1,15)); Menu1.setEnabled(false); Menu1.addMouseListener(this); Menu_Jpanel.add(Menu1); Menu2=new JLabel("预约培训",new ImageIcon("image/Pre_book.jpg"),JLabel.CENTER); Menu2.setCursor(myCursor); Menu2.setFont(new Font("预约培训", 1,15)); Menu2.setEnabled(false); Menu2.addMouseListener(this); Menu_Jpanel.add(Menu2); Menu3=new JLabel("电子资源",new ImageIcon("image/e-resources.jpg"),JLabel.CENTER); Menu3.setCursor(myCursor); Menu3.setFont(new Font("电子资源", 1,15)); Menu3.setEnabled(false); Menu3.addMouseListener(this); Menu_Jpanel.add(Menu3); Menu4=new JLabel("书库检索",new ImageIcon("image/DataBase_retrieval.jpg"),JLabel.CENTER); Menu4.setCursor(myCursor); Menu4.setFont(new Font("书库检索", 1,15)); Menu4.setEnabled(false); Menu4.addMouseListener(this); Menu_Jpanel.add(Menu4); Menu5=new JLabel("好书收藏",new ImageIcon("image/collect_book.jpg"),JLabel.CENTER); Menu5.setCursor(myCursor); Menu5.setFont(new Font("好书收藏", 1,15)); Menu5.setEnabled(false); Menu5.addMouseListener(this); Menu_Jpanel.add(Menu5); Menu6=new JLabel("个人资料",new ImageIcon("image/Personal _info.jpg"),JLabel.CENTER); Menu6.setCursor(myCursor); Menu6.setFont(new Font("个人资料", 1,15)); Menu6.setEnabled(false); Menu6.addMouseListener(this); Menu_Jpanel.add(Menu6); inter_Jpanel.setLayout(new BorderLayout()); Search_Jpanel.setLayout(new GridLayout(2, 5,50,40)); Search_Jpanel.add(new JPanel());//起间隔作用 Search_Key=new JComboBox<>(key); Search_Jpanel.add(Search_Key); key_name=new JTextField(20); Search_Jpanel.add(key_name); Search_Button=new JButton("检索"); Search_Button.addActionListener(this); Search_Jpanel.add(Search_Button); Search_Jpanel.add(new JPanel());//起间隔作用 Search_Jpanel.add(new JPanel());//起间隔作用 Search_Jpanel.add(new JPanel());//起间隔作用 Search_Jpanel.add(new JPanel());//起间隔作用 Search_Jpanel.add(new JPanel());//起间隔作用 Search_Jpanel.add(new JPanel());//起间隔作用 inter_Jpanel.add(Search_Jpanel,BorderLayout.NORTH); List_Jpanel=new Show_BookList(socket,Read_in,Write_out); inter_Jpanel.add(List_Jpanel, BorderLayout.CENTER); Main_Jpanel.setLayout(new BorderLayout()); Spacer_bar=new JLabel(new ImageIcon("image/spacer_bar.jpg")); Main_Jpanel.add(Spacer_bar,BorderLayout.WEST); Main_Jpanel.add(inter_Jpanel,BorderLayout.CENTER); } private void initialFrame() { this.setTitle("图书管理系统--客户端");//设置窗体标题 Image image=new ImageIcon("./image/library_ico.jpg").getImage(); this.setIconImage(image); Dimension scrSize=Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕长宽 this.setBounds(scrSize.width/6,scrSize.height/6,1000,700); this.add(title_Jpanel,BorderLayout.NORTH); this.add(Menu_Jpanel,BorderLayout.WEST); this.add(Main_Jpanel,BorderLayout.CENTER); this.setVisible(true);//设置可见性 this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(mywindowadapter); } public WindowAdapter mywindowadapter=new WindowAdapter() { public void windowClosing(WindowEvent e) { try { IOout.SendMessage(Write_out, Order_format.Exit);//通知服务器退出 } catch (IOException e1) { e1.printStackTrace(); } dispose(); }; }; @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==this.Search_Button){//检索按键 try { Search_Book(); } catch (IOException | ClassNotFoundException e1) {} } } public void Search_Book() throws IOException, ClassNotFoundException{ List_Jpanel.Isclick_ture(); switch (Search_Key.getSelectedIndex()) { case 0: Got_Allbook();break; case 1:Search_Gotbook(Order_format.Callno);break; case 2:Search_Gotbook(Order_format.bookname);break; case 3:Search_Gotbook(Order_format.author);break; case 4:Search_Gotbook(Order_format.publishment);break; default: break; } } @SuppressWarnings("unchecked") public void Got_Allbook() throws IOException, ClassNotFoundException{ IOout.SendMessage(Write_out, Order_format.All_book+key_name.getText().trim()); Vector<Book_Info> vector=(Vector<Book_Info>) IOin.ReceiveMessage(Read_in); List_Jpanel.Update(vector); } public void Search_Gotbook(String order) throws IOException, ClassNotFoundException{ String msg=key_name.getText().trim(); if(msg.length()>0){ IOout.SendMessage(Write_out, order+msg); Vector<Book_Info> vector=(Vector<Book_Info>) IOin.ReceiveMessage(Read_in); if(vector.size()!=0) List_Jpanel.Update(vector); else{ List_Jpanel.clear(); JOptionPane.showMessageDialog(this,"木有查询结果!","提醒",JOptionPane.ERROR_MESSAGE); } }else{ JOptionPane.showMessageDialog(this,"输入框不能为空!","错误",JOptionPane.ERROR_MESSAGE); } } @Override public void mouseClicked(MouseEvent e) { if(e.getSource()==this.Menu1){ try { IOout.SendMessage(Write_out, Order_format.User_book+this.user_ID); Vector<Book_Info> vector=(Vector<Book_Info>) IOin.ReceiveMessage(Read_in); List_Jpanel.Update(vector); List_Jpanel.Isclick_false(); } catch (IOException e1) { e1.printStackTrace(); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } }else if(e.getSource()==this.Menu2){ }else if(e.getSource()==this.Menu3){ }else if(e.getSource()==this.Menu4){ }else if(e.getSource()==this.Menu5){ }else if(e.getSource()==this.Menu6){ } } @Override public void mouseEntered(MouseEvent e) { if(e.getSource()==this.Menu1){ Menu1.setEnabled(true); }else if(e.getSource()==this.Menu2){ Menu2.setEnabled(true); }else if(e.getSource()==this.Menu3){ Menu3.setEnabled(true); }else if(e.getSource()==this.Menu4){ Menu4.setEnabled(true); }else if(e.getSource()==this.Menu5){ Menu5.setEnabled(true); }else if(e.getSource()==this.Menu6){ Menu6.setEnabled(true); } } @Override public void mouseExited(MouseEvent e) { if(e.getSource()==this.Menu1){ Menu1.setEnabled(false); }else if(e.getSource()==this.Menu2){ Menu2.setEnabled(false); }else if(e.getSource()==this.Menu3){ Menu3.setEnabled(false); }else if(e.getSource()==this.Menu4){ Menu4.setEnabled(false); }else if(e.getSource()==this.Menu5){ Menu5.setEnabled(false); }else if(e.getSource()==this.Menu6){ Menu6.setEnabled(false); } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } }

    查询功能以及借书还书功能

    package Library_Client; public class Show_BookList extends JPanel { Socket socket; ObjectOutputStream Write_out; ObjectInputStream Read_in; IO_in IOin=new IO_in(); IO_out IOout=new IO_out(); boolean Isclick; Vector<String> head = new Vector<String>(); {//定义表头 head.add("书名");head.add("作者");head.add("出版社"); head.add("状态");head.add("索引号"); } Vector<Vector> data=new Vector<Vector>();//定义检索出的书的基本信息 DefaultTableModel TableModel=new DefaultTableModel(data,head); //创建表格模型 JTable head_table=new JTable(TableModel){ public boolean isCellEditable(int row, int column) { //表格不可编辑 return false; } }; //创建Jtable对象 JScrollPane ScrollPane=new JScrollPane(head_table);//将JTable封装到滚动窗格 public Show_BookList(Socket socket,ObjectInputStream Read_in,ObjectOutputStream Write_out){ this.socket=socket; this.Read_in=Read_in; this.Write_out=Write_out; this.setLayout(new BorderLayout()); head_table.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { if(e.getClickCount()==2){ if(head_table.getValueAt(head_table.getSelectedRow(), 3).toString().equals("在架上")){//判断是否已借出 Object value= head_table.getValueAt(head_table.getSelectedRow(), 0);//得到选中的单元格的值,表格中都是字符串 try { Correct_Borrow(JOptionPane.showConfirmDialog (null,"是否借阅 “"+value.toString()+"” 这本书?","提示", JOptionPane.YES_NO_OPTION),value.toString()); } catch (HeadlessException | IOException e1) { e1.printStackTrace(); } }else{ if(Isclick) JOptionPane.showMessageDialog(null,"该书已借出!","提醒",JOptionPane.ERROR_MESSAGE); else{ Object value= head_table.getValueAt(head_table.getSelectedRow(), 0);//得到选中的单元格的值,表格中都是字符串 try { Correct_return(JOptionPane.showConfirmDialog (null,"是否还 “"+value.toString()+"” 这本书?","提示", JOptionPane.YES_NO_OPTION),value.toString()); } catch (HeadlessException | IOException e1) { e1.printStackTrace(); } } } } } }); this.add(ScrollPane,BorderLayout.NORTH); this.setVisible(true); } public void Update(Vector<Book_Info> vector){ Vector<Vector> data=new Vector<Vector>(); Vector<String> a; for(int i=0;i<vector.size();i++){ a=new Vector<String>(); a.addElement(vector.get(i).getBookname()); a.addElement(vector.get(i).getAuthor()); a.addElement(vector.get(i).getPublishment()); a.addElement(vector.get(i).getState()); a.addElement(vector.get(i).getCallno()); data.addElement(a); } TableModel.setDataVector(data,head); head_table.updateUI(); head_table.repaint(); } public void clear(){ Vector<Vector> data=new Vector<Vector>(); TableModel.setDataVector(data,head); head_table.updateUI(); head_table.repaint(); } public void Correct_Borrow(int a,String msg) throws IOException{ if(a==0){ msg=Order_format.Borrow_Book+msg; IOout.SendMessage(Write_out,msg); try { Object ms=(Object) IOin.ReceiveMessage(Read_in); if(ms instanceof String){//判断接收数据的类型 String m=(String)ms; JOptionPane.showMessageDialog(this,m.substring(11),"提醒",JOptionPane.ERROR_MESSAGE); }else Update((Vector<Book_Info>)ms);//刷新数据 } catch (ClassNotFoundException e) {} } } public void Correct_return(int a,String msg) throws IOException{ if(a==0){ msg=Order_format.Return_Book+msg; IOout.SendMessage(Write_out,msg); try { Update((Vector<Book_Info>)IOin.ReceiveMessage(Read_in)); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } public void Isclick_false(){ Isclick=false; } public void Isclick_ture(){ Isclick=true; } } 其余代码就不贴出来了,有兴趣可以自己下载!

    (4)移动客户端

    源码下载
    转载请注明原文地址: https://ju.6miu.com/read-15279.html

    最新回复(0)