zoukankan      html  css  js  c++  java
  • java实现类似qq的窗口对聊

      Swing实现界面,socket实现通信

    package com.learn.talk;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    
    public class ChatReceiver {
    
        private PrintWriter pw;
        private JFrame frame;
        private JPanel pane_buttom;
        private JSplitPane pane_center;
    
        // 显示内容的文本框,输入内容的文本框,发送内容按钮
        private JScrollPane pane_showWindow;
        private JScrollPane pane_inputWindow;
        private JTextArea area_showWindow;
        private JTextArea area_inputWindow;
    
        private JButton btn_send;
    
        private Dimension dimension;// 用于设置area_showWindow可拖拉的大小
    
        // 初始化
        public ChatReceiver() {
            frame = new JFrame();
            pane_buttom = new JPanel();
            pane_showWindow = new JScrollPane();
            pane_inputWindow = new JScrollPane();
            area_showWindow = new JTextArea();
            area_inputWindow = new JTextArea();
            pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false,
                    pane_showWindow, pane_inputWindow);
            btn_send = new JButton("send");
    
            dimension = new Dimension(50, 300);
    
        }
    
        // 调用方法显示窗口
        public void showFrame() {
            initFrame();
            initChatTextArea();
            initButton();
            btn_send();
            socket();
        }
    
        // 主窗体
        public void initFrame() {
            frame.setTitle("简繁");
            int width = (int) Toolkit.getDefaultToolkit().getScreenSize()
                    .getWidth();
            int height = (int) Toolkit.getDefaultToolkit().getScreenSize()
                    .getHeight();
            frame.setBounds(width / 2, height / 2, 400, 450);
            frame.setVisible(true);
        }
    
        // 内容显示文本框和输入内容文本框
        private void initChatTextArea() {
            // 取得视图焦点
            pane_showWindow.getViewport().add(area_showWindow);
            pane_inputWindow.getViewport().add(area_inputWindow);
            // 将显示文本域设置为不可编辑
            area_showWindow.setEditable(false);
            // 设置显示文本域可拖拉的大小
            pane_showWindow.setMinimumSize(dimension);
            frame.add(pane_center, BorderLayout.CENTER);
        }
    
        // 发送文件,发送内容按钮
        public void initButton() {
            pane_buttom.add(btn_send);
            frame.add(pane_buttom, BorderLayout.SOUTH);
        }
    
        private void btn_send() {
            btn_send.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String info = area_inputWindow.getText();
                    area_showWindow.append("简繁:" + info + "
    ");
                    pw.println(info);
                    area_inputWindow.setText("");
                }
            });
        }
    
        private void socket() {
            ServerSocket ss;
            try {
                ss = new ServerSocket(9988);
                // 等待连接 客户端
                Socket s = ss.accept();
                InputStreamReader isr = new InputStreamReader(s.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                // PrintWriter必须和socket有密切的关系
                pw = new PrintWriter(s.getOutputStream(), true);
    
                // 读取从客户端法发来的信息
                while (true) {
                    // 读取从客户端发来的信息
                    String info = br.readLine();
                    // 在文本栏里显示
                    area_showWindow.append("garfield:" + info + "
    ");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            ChatReceiver chat = new ChatReceiver();
            chat.showFrame();
        }
    }
    package com.learn.talk;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    
    public class ChatSender {
        private PrintWriter pw;
        private JFrame frame;
        private JPanel pane_buttom;
        private JSplitPane pane_center;
    
        // 显示内容的文本框,输入内容的文本框,发送内容按钮
        private JScrollPane pane_showWindow;
        private JScrollPane pane_inputWindow;
        private JTextArea area_showWindow;
        private JTextArea area_inputWindow;
    
        private JButton btn_send;
    
        private Dimension dimension;// 用于设置area_showWindow可拖拉的大小
    
        // 初始化
        public ChatSender() {
            frame = new JFrame();
            pane_buttom = new JPanel();
            pane_showWindow = new JScrollPane();
            pane_inputWindow = new JScrollPane();
            area_showWindow = new JTextArea();
            area_inputWindow = new JTextArea();
            pane_center = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false,
                    pane_showWindow, pane_inputWindow);
            btn_send = new JButton("send");
    
            dimension = new Dimension(50, 300);
        }
    
        // 调用方法显示窗口
        public void showFrame() {
            initFrame();
            initChatTextArea();
            initButton();
            btn_send();
            socket();
        }
    
        // 主窗体
        public void initFrame() {
            frame.setTitle("garfield");
            int width = (int) Toolkit.getDefaultToolkit().getScreenSize()
                    .getWidth();
            int height = (int) Toolkit.getDefaultToolkit().getScreenSize()
                    .getHeight();
            frame.setBounds(width / 2, height / 2, 400, 450);
            frame.setVisible(true);
        }
    
        // 内容显示文本框和输入内容文本框
        private void initChatTextArea() {
            // 取得视图焦点
            pane_showWindow.getViewport().add(area_showWindow);
            pane_inputWindow.getViewport().add(area_inputWindow);
            // 将显示文本域设置为不可编辑
            area_showWindow.setEditable(false);
            // 设置显示文本域可拖拉的大小
            pane_showWindow.setMinimumSize(dimension);
            frame.add(pane_center, BorderLayout.CENTER);
        }
    
        // 发送文件,发送内容按钮
        public void initButton() {
            pane_buttom.add(btn_send);
            frame.add(pane_buttom, BorderLayout.SOUTH);
        }
    
        private void btn_send() {
            btn_send.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String info = area_inputWindow.getText();
                    area_showWindow.append("garfield: " + info + "
    ");
                    pw.println(info);
                    area_inputWindow.setText("");
                }
            });
        }
    
        private void socket() {
            try {
                Socket s = new Socket("127.0.0.1", 9988);
                InputStreamReader isr = new InputStreamReader(s.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                pw = new PrintWriter(s.getOutputStream(), true);
                while (true) {
                    String info = br.readLine();
                    area_showWindow.append("简繁: " + info + "
    ");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            ChatSender chat = new ChatSender();
            chat.showFrame();
        }
    }
  • 相关阅读:
    Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
    Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识
    Hibernate HQL语句
    spring的IO原理
    jsp概述
    java的常用接口
    java各种内部类
    Serlvet 处理http请求并保持长连接
    JVM高级特性与实践(一):Java内存区域 与 内存溢出异常
    JVM高级特性与实践(二):对象存活判定算法(引用) 与 回收
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/5577498.html
Copyright © 2011-2022 走看看