天天看點

圖書管理系統圖書管理系統

圖書管理系統

該圖書管理系統是我研一時候的一個課程設計,當時也剛重新學完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)移動用戶端

圖書管理系統圖書管理系統

源碼下載下傳