黑马程序员技术交流社区

标题: 我用java做的QQ聊天系统 [打印本页]

作者: IT我的梦    时间: 2015-4-16 00:01
标题: 我用java做的QQ聊天系统
/*
*简易QQ聊天系统
*客户端
*
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;  
import java.net.*;  
class QQChatSystem1
{
        JFrame qqframe=new JFrame("QQ聊天系统-客户端");
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();
        JPanel panel3=new JPanel();
        JTextArea sendInfor=new JTextArea(5,26);
        JTextArea receiveInfor=new JTextArea(15,25);
        JButton send=new JButton("发送");
        JButton cancel=new JButton("取消");
        JButton exit=new JButton("退出");
       
        JScrollPane scrollpane=new JScrollPane(receiveInfor);//创建一个滚动条窗格,并且将receiveInfor文本域添加到该滚动条窗格中。
        JScrollPane scrollpane2=new JScrollPane(sendInfor);//创建一个滚动条窗格,并且将sendInfor文本域添加到该滚动条窗格中。
       
        Send sendlistener=new Send();
        Exit exitlistener=new Exit();
        Cancel cancellistener=new Cancel();
       
        String i;
        Socket s;
        InputStream is;
        DataInputStream dis;
        OutputStream os;
        DataOutputStream dos;
       
        QQChatSystem1() throws Exception
        {
                receiveInfor.setEditable(false);
                qqframe.setLayout(new FlowLayout());
                panel3.setLayout(new GridLayout(1,3,50,5));
                panel1.setBorder(BorderFactory.createTitledBorder("qq消息"));
                panel2.setBorder(BorderFactory.createTitledBorder("发送消息"));
               
               
                qqframe.add(panel1);
                qqframe.add(panel2);
                qqframe.add(panel3);
                panel1.add(scrollpane);
                scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);//设置水平滚动条总是显示。
                scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//设置垂直滚动条总是显示。
                panel2.add(scrollpane2);
                panel3.add(send);
                panel3.add(cancel);
                panel3.add(exit);
               
                send.addActionListener(sendlistener);
                exit.addActionListener(exitlistener);
                cancel.addActionListener(cancellistener);
               
                qqframe.setSize(350,550);
                qqframe.setVisible(true);
                qqframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                qqframe.setLocationRelativeTo(null);
                qqframe.setResizable(false);
               
                        s=new Socket("127.0.0.1",8888);
                           os=s.getOutputStream();
                        dos=new DataOutputStream(os);
                        is=s.getInputStream();
                        dis=new DataInputStream(is);
        }
       
        class Send implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==(JButton)send)
                        {
                                try
                                 {
                                         i=sendInfor.getText();
                                        dos.writeUTF(i);
                                        receiveInfor.append("客户端:"+i+"\n");
                                        sendInfor.setText("");
                                 }
                                 catch (Exception ee)
                                 {  
                                        System.out.println("Error....." + ee);  
                             }  
                        }
                       
                }
        }
        class Exit implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        try
                        {
                            if(e.getSource()==(JButton)exit)
                                {
                                System.exit(0);
                                        dos.close();
                                        s.close();
                                        dis.close();       
                                }
                        }
                        catch(Exception ee)
                        {
                               
                        }
                }
        }
        class Cancel implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==(JButton)cancel)
                        {
                                sendInfor.setText("");
                                sendInfor.requestFocusInWindow();       
                        }
                }
        }
        void receive()throws Exception
        {
                while(true)
                {
                    receiveInfor.append("服务器端:"+dis.readUTF()+"\n");       
                }
        }
       
        public static void main(String[] args) throws Exception
        {
                QQChatSystem1 qqchat=new QQChatSystem1();
                qqchat.receive();
        }
}



/*
*简易QQ聊天系统
*服务器端
*
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;  
import java.net.*;  
class QQChatSystem2
{
        JFrame qqframe=new JFrame("QQ聊天系统-服务器端");
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();
        JPanel panel3=new JPanel();
        JTextArea sendInfor=new JTextArea(5,26);
        JTextArea receiveInfor=new JTextArea(15,25);
        JButton send=new JButton("发送");
        JButton cancel=new JButton("取消");
        JButton exit=new JButton("退出");
       
        JScrollPane scrollpane=new JScrollPane(receiveInfor);//创建一个滚动条窗格,并且将receiveInfor文本域添加到该滚动条窗格中。
        JScrollPane scrollpane2=new JScrollPane(sendInfor);//创建一个滚动条窗格,并且将sendInfor文本域添加到该滚动条窗格中。
       
        Send sendlistener=new Send();
        Exit exitlistener=new Exit();
        Cancel cancellistener=new Cancel();
       
        String i;
        InputStream in;
        DataInputStream dis;
        OutputStream os;
        ServerSocket ss;
        Socket s;
        DataOutputStream dos;
        QQChatSystem2() throws Exception
        {
                receiveInfor.setEditable(false);
                qqframe.setLayout(new FlowLayout());
                panel3.setLayout(new GridLayout(1,3,50,5));
                panel1.setBorder(BorderFactory.createTitledBorder("qq消息"));
                panel2.setBorder(BorderFactory.createTitledBorder("发送消息"));
               
                qqframe.add(panel1);
                qqframe.add(panel2);
                qqframe.add(panel3);
                panel1.add(scrollpane);
                scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);//设置水平滚动条总是显示。
                scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//设置垂直滚动条总是显示。
                panel2.add(scrollpane2);
                panel3.add(send);
                panel3.add(cancel);
                panel3.add(exit);
               
                send.addActionListener(sendlistener);
                exit.addActionListener(exitlistener);
                cancel.addActionListener(cancellistener);
               
                qqframe.setSize(350,550);
                qqframe.setVisible(true);
                qqframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                qqframe.setLocationRelativeTo(null);
                qqframe.setResizable(false);
               
                 ss=new ServerSocket(8888);
                 s=ss.accept();
                 in=s.getInputStream();
                 dis=new DataInputStream(in);
                 os=s.getOutputStream();
                 dos=new DataOutputStream(os);
        }
       
        class Send implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==(JButton)send)
                        {
                                try
                                {
                                        i=sendInfor.getText();
                                        dos.writeUTF(i);
                                        receiveInfor.append("服务器端:"+i+"\n");
                                        sendInfor.setText("");
                                }
                                catch(Exception ex)
                                {
                                       
                                }
                               
                     }
                }
                       
        }
        class Exit implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        try
                        {
                                if(e.getSource()==(JButton)exit)
                                {
                                System.exit(0);       
                                                s.close();
                                }
                        }
                        catch(Exception ee)
                        {
                               
                        }
                }
        }
        class Cancel implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==(JButton)cancel)
                        {
                                sendInfor.setText("");
                                sendInfor.requestFocusInWindow();       
                        }
                }
        }
                void receive()throws Exception
        {
                while(true)
                {
                    receiveInfor.append("客户端:"+dis.readUTF()+"\n");       
                }
        }
       
        public static void main(String[] args) throws Exception
        {
                QQChatSystem2 qqchat=new QQChatSystem2();
                qqchat.receive();
        }
}


QQ聊天系统.jpg (119.73 KB, 下载次数: 4)

QQ聊天系统

QQ聊天系统

作者: 静心明德    时间: 2015-4-16 00:11
兄弟,技术很牛啊,我们一起努力!:handshake
作者: cody    时间: 2015-4-16 00:13
楼主正在黑马上课吗?
作者: kolyneh    时间: 2015-4-16 00:23
不错不错,厉害啊
作者: 嗯_来吧    时间: 2015-4-16 00:27
顶一个,真厉害
作者: Zack    时间: 2015-4-16 00:34
大神 学习一下
作者: mono    时间: 2015-4-16 09:39
好高大上啊、
作者: IT我的梦    时间: 2015-4-18 13:48
呵呵呵,大神可不敢当,我也是准备考黑马的
作者: doomsday    时间: 2015-4-18 14:26
IT我的梦 发表于 2015-4-18 13:48
呵呵呵,大神可不敢当,我也是准备考黑马的

这是打算多少期
作者: zdh    时间: 2015-4-18 14:27
还不错的感觉哦
作者: 知来者之可追    时间: 2015-4-18 14:37
哇哦,赞一个
作者: 晓月清晖明    时间: 2015-4-18 14:40
哇哦,大神,好NB的样子




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2