《Java课程设计(qq聊天程序).ppt》由会员分享,可在线阅读,更多相关《Java课程设计(qq聊天程序).ppt(33页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、MiniQQtian_Using Java设计文档可视化多方通信好友维护聊天记录开发文档、总结报告登录窗口注册窗口好友列表聊天窗口查找/添加好友聊天记录对话框(登录失败、注册成功、注册失败 确认删除、错误提示等)登录窗口注册查找好友好友列表查找结果聊天窗口聊天记录PackagesServer登录验证注册服务好友维护聊天组维护在线用户维护消息转发异常处理.Client各个窗口好友维护聊天记录维护活动窗口维护异常处理.Multi-ThreadSocket-ServerSocketI/O StreamJDBC (Files R/W)创建Thread的子类,并覆盖run()方法实现Runnable接口
2、public class ChatServer ArrayList clients=new ArrayList();public static void main(String args)new ChatServer().begin();public void begin()ss=new ServerSocket(8888);Socket s=ss.accept();Client c=new Client(s);new Thread(c).start();clients.add(c);class Client implements Runnable /ChatServer内部类内部类priva
3、te Socket s;private DataInputStream dis=null;private DataOutputStream dos=null;public Client(Socket s)this.s=s;dis=new DataInputStream(s.getInputStream();dos=new DataOutputStream(s.getOutputStream();public void send(String str)dos.writeUTF(str);public void run()String str=dis.readUTF();for(int i=0;i
4、 SHOW DATABASES;2:创建一个数据库MYSQLDATA mysql CREATE DATABASE MYSQLDATA;3:选择你所创建的数据库 mysql USE MYSQLDATA;(按回车键出现Database changed 时说明操作成功!)4:查看现在的数据库中存在什么表 mysql SHOW TABLES;5:创建一个数据库表 mysql CREATE TABLE MYTABLE(name VARCHAR(20),sex CHAR(1);6:显示表的结构:mysql DESCRIBE MYTABLE;7:往表中加入记录 mysql insert into MYTAB
5、LE values(hyq,M);8:用文本方式将数据装入数据库表中(例如D:/mysql.txt)mysql LOAD DATA LOCAL INFILE D:/mysql.txt INTO TABLE MYTABLE;9:导入.sql文件命令(例如D:/mysql.sql)mysqluse database;mysqlsource d:/mysql.sql;10:删除表 mysqldrop TABLE MYTABLE;11:清空表 mysqldelete from MYTABLE;12:更新表中数据 mysqlupdate MYTABLE set sex=f where name=hyq;
6、MySQL-front Navicat SQLyog mysql-workbenchuserIDnamepasswordregDate.friendshipIDuser_IDfriend_IDtypefrdDate.recordIDuser_IDfriend_IDother info.LoginfriendListfriendManagementchatWindowgroupManagementchatRecordLogin.javabtnLogin.addActionListener(new ActionListener()public void actionPerformed(Action
7、Event e)boolean checked=server.verify(this.getuser(),this.password();/登录验证登录验证if(checked)new friendsWindow();loginFrame.setVisible(false);/loginFrame.dispose();else.);listFriend()/server.javaStatement s=conn.createStatement();ResultSet rs=s.executeQuery(select user.name,friendship.type +from user,fr
8、iendship+where user.ID=friendship.friend_ID+AND friendship.user_ID=+this.getUserID()+);while(rs.next()int type=rs.getInt(type);swith(type)case 0:/friendNode.add(current);case 1:/strangeNode.add(current);default:/blacklistNode.add(current);每一个chatWindow对应一个线程所有chatWindow按照组进行分类,并且由server负责维护讨论组每个讨论组里
9、面都应该有一个数据结构(list)维护组里的窗口服务器收发消息时向数据库中插入聊天记录,或者本地维护另一个数据库,在本地插入聊天记录采用文本文件保存聊天记录时,可以以对方用户名/ID为文件名分别保存不同对象的聊天记录 private BufferedReader inputStream=null;private BufferedWriter outputStream=null;private StringBuilder sb=new StringBuilder();private String file;private int line=0;/number zero private stati
10、c SimpleDateFormat s=new SimpleDateFormat(yyyy-MM-dd);/格式化日期使用public class FileManagerpublic FileManager(String userID,String friendID)checkExsit(userID,friendID);run();private void checkExsit(String userID,String friendID)/*检查用户聊天记录文件夹路径存在否,*userID为当前用户,friendID为好友*/File f=new File(System.getProper
11、ties().getProperty(user.dir)+chatLog/+userID);if(!f.exists()f.mkdirs();file=f.getAbsolutePath()+/+friendID+.txt;f=new File(file);if(!f.exists()/*检查聊天记录文件有无检查聊天记录文件有无*/try f.createNewFile();catch(IOException e)e.printStackTrace();public void run()try inputStream=new BufferedReader(new FileReader(file
12、);String l;while(l=inputStream.readLine()!=null)line+;sb.append(l+System.getProperty(line.separator);catch(FileNotFoundException e)e.printStackTrace();catch(IOException e)e.printStackTrace();finally if(inputStream!=null)try inputStream.close();catch(IOException e)e.printStackTrace();/获取当前日期.格式为 2009
13、-02-02private static String getDate()Date date=new Date();return +s.format(date)+;/清除记录public void removeMsg()line=0;sb.delete(0,getString().length();/保存聊天记录.public void save(String msg)String newMsg=msg;/加入当前日期.if(msg.trim().length()!=0)newMsg=getDate()+System.getProperty(line.separator)+-+System.g
14、etProperty(line.separator)+newMsg;sb.append(System.getProperty(line.separator)+newMsg);try outputStream=new BufferedWriter(new FileWriter(file);outputStream.write(sb.toString();catch(IOException e)e.printStackTrace();finally try outputStream.close();catch(IOException e)e.printStackTrace();/获取记录内容pub
15、lic String getString()return sb.toString();/获取行数public int getLines()return line;private JTree tree;private TreeNode node;private DefaultTreeModel model;private DefaultTreeCellRenderer renderer;model=new DefaultTreeModel(node);tree=new JTree(model);renderer=new DefaultTreeCellRenderer();renderer.set
16、LeafIcon(new ImageIcon(a.jpg);/叶子结点图片 renderer.setClosedIcon(new ImageIcon(b.jpg);/关闭树后显示的图片 renderer.setOpenIcon(new ImageIcon(c.jpg);/打开树时显示的图片 tree.setCellRenderer(renderer);头像显示public class TreePopupMenu extends JFrame implements MouseListener,ActionListener private static final long serialVersi
17、onUID=1L;JTree tree;JPopupMenu popMenu;JMenuItem addItem;JMenuItem delItem;JMenuItem editItem;public TreePopupMenu()/JTree构造 /String model=我的好友,陌生人,黑名单;/tree=new JTree(model);tree=new JTree();tree.setEditable(true);tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);t
18、ree.addMouseListener(this);JTree右键菜单 tree.setCellEditor(new DefaultTreeCellEditor(tree,new DefaultTreeCellRenderer();setSize(200,800);popMenu=new JPopupMenu();addItem=new JMenuItem(加入黑名单加入黑名单);addItem.addActionListener(this);delItem=new JMenuItem(删除好友删除好友);delItem.addActionListener(this);editItem=ne
19、w JMenuItem(修改备注修改备注);editItem.addActionListener(this);popMenu.add(addItem);popMenu.add(delItem);popMenu.add(editItem);getContentPane().add(new JScrollPane(tree);public void mouseClicked(MouseEvent e)public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mousePressed(
20、MouseEvent e)TreePath path=tree.getPathForLocation(e.getX(),e.getY();if(path=null)return;tree.setSelectionPath(path);if(e.getButton()=3)popMenu.show(tree,e.getX(),e.getY();public void mouseReleased(MouseEvent e)public void actionPerformed(ActionEvent e)DefaultMutableTreeNode node=(DefaultMutableTree
21、Node)tree.getLastSelectedPathComponent();if(e.getSource()=addItem)(DefaultTreeModel)tree.getModel().insertNodeInto(new DefaultMutableTreeNode(Test),node,node.getChildCount();tree.expandPath(tree.getSelectionPath();else if(e.getSource()=delItem)if(node.isRoot()return;(DefaultTreeModel)tree.getModel().removeNodeFromParent(node);else if(e.getSource()=editItem)tree.startEditingAtPath(tree.getSelectionPath();public static void main(String args)TreePopupMenu frame=new TreePopupMenu();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);