A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© IT我的梦 中级黑马   /  2015-4-16 00:01  /  1063 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
*简易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, 下载次数: 2)

QQ聊天系统

QQ聊天系统

11 个回复

倒序浏览
兄弟,技术很牛啊,我们一起努力!:handshake
回复 使用道具 举报
楼主正在黑马上课吗?
回复 使用道具 举报
不错不错,厉害啊
回复 使用道具 举报
顶一个,真厉害
回复 使用道具 举报
大神 学习一下
回复 使用道具 举报
mono 中级黑马 2015-4-16 09:39:22
7#
好高大上啊、
回复 使用道具 举报
呵呵呵,大神可不敢当,我也是准备考黑马的
回复 使用道具 举报
IT我的梦 发表于 2015-4-18 13:48
呵呵呵,大神可不敢当,我也是准备考黑马的

这是打算多少期
回复 使用道具 举报
zdh 中级黑马 2015-4-18 14:27:37
10#
还不错的感觉哦
回复 使用道具 举报
哇哦,赞一个
回复 使用道具 举报
哇哦,大神,好NB的样子
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马