java-模拟qq聊天的源代码

上传人:沈*** 文档编号:113798982 上传时间:2022-06-27 格式:DOC 页数:15 大小:50.50KB
返回 下载 相关 举报
java-模拟qq聊天的源代码_第1页
第1页 / 共15页
java-模拟qq聊天的源代码_第2页
第2页 / 共15页
java-模拟qq聊天的源代码_第3页
第3页 / 共15页
点击查看更多>>
资源描述
java-模拟qq聊天的源代码客户端:package mutil;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;import javax.swing.JTextArea;public class ClienThread extends Threadprivate Socket socket = null;private javax.swing.JTextArea jTextArea1;public ClienThread(JTextArea jTextArea1)this.jTextArea1 = jTextArea1;public void run()try/ 创建连接socket = new Socket(, 12344);String message = null;while (true)/ 接受服务端请求message = receive();/ 显示到消息接收框if (message != null & !message.equals()if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(服务端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n服务端: + message); elsemessage = 服务端以断开连接或未连接!;if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(服务端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n服务端: + message);break; catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 向服务端发送消息 */public void send(String message)tryPrintWriter printWriter = new PrintWriter(socket.getOutputStream();printWriter.write(message + n);printWriter.flush(); catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 接受服务端消息 */public String receive()String message = ;tryBufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream();message = bufferedReader.readLine(); catch (IOException e)System.out.println(服务端已断开或未连接!);return message;服务端:package mutil;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .ServerSocket;import .Socket;import javax.swing.JTextArea;public class ServerThread extends Threadprivate Socket socket = null;private ServerSocket serverSocket = null;private javax.swing.JTextArea jTextArea1;public ServerThread(JTextArea jTextArea1)this.jTextArea1 = jTextArea1;public void run()try/ 创建连接serverSocket = new ServerSocket(12344);/ 监听客户连接socket = serverSocket.accept();String message = null;while (true)/ 接受客户请求message = receive(socket);if (message != null & !message.equals()/ 显示到消息接收框if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(客户端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n客户端: + message); elsemessage = 客户端以断开连接或未连接!;if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(客户端: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText()+ n客户端: + message);break; catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 向客户端发送消息 */public void send(String message)tryPrintWriter printWriter = new PrintWriter(socket.getOutputStream();printWriter.write(message + n);printWriter.flush(); catch (IOException e)/ TODO Auto-generated catch blocke.printStackTrace();/* * 接受客户端消息 */public String receive(Socket socket)String message = ;tryBufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream();message = bufferedReader.readLine(); catch (IOException e)System.out.println(服务端已断开或未连接!);return message;服务端界面:/* * SereveFrame.java * * Created on _DATE_, _TIME_ */package com.insigma.frame;import mutil.ServerThread;/* * * author _USER_ */public class ServerFrame extends javax.swing.JFrameprivate ServerThread serverThread;/* Creates new form SereveFrame */public ServerFrame()initComponents();serverThread = new ServerThread(this.jTextArea1);serverThread.start();/* * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ GEN-BEGIN:initComponents/ private void initComponents()jScrollPane1 = new javax.swing.JScrollPane();jTextArea1 = new javax.swing.JTextArea();jScrollPane2 = new javax.swing.JScrollPane();jTextArea2 = new javax.swing.JTextArea();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle(u670du52a1u7aef);setBackground(new java.awt.Color(153, 153, 255);jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumns(20);jTextArea2.setRows(5);jScrollPane2.setViewportView(jTextArea2);jButton1.setText(u53d6u6d88);jButton1.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton1ActionPerformed(evt););jButton2.setText(u53d1u9001);jButton2.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton2ActionPerformed(evt););javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane();getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComponent(jButton1).addGap(18, 18,18).addComponent(jButton2).addGap(35, 35,35).addGroup(layout.createSequentialGroup().addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,377,javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComponent(jScrollPane2,javax.swing.GroupLayout.DEFAULT_SIZE,377,Short.MAX_VALUE).addContainerGap();layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,242,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton2).addComponent(jButton1).addContainerGap();pack();/ / GEN-END:initComponents/* * 取消 * * param evt */private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)this.jTextArea2.setText();/* * 发送 * * param evt */private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)/ 向客户端发消息String message = this.jTextArea2.getText();if (message != null & !message.equals()serverThread.send(message);/ 清空消息输入框this.jTextArea2.setText();/ 显示到消息接收框if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(我说: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText() + n我说:+ message);/* * param args * the command line arguments */public static void main(String args)java.awt.EventQueue.invokeLater(new Runnable()public void run()new ServerFrame().setVisible(true););/ GEN-BEGIN:variables/ Variables declaration - do not modifyprivate javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JScrollPane jScrollPane2;private javax.swing.JTextArea jTextArea1;private javax.swing.JTextArea jTextArea2;/ End of variables declaration/GEN-END:variables客户端界面:/* * ClienFrame.java * * Created on _DATE_, _TIME_ */package com.insigma.frame;import mutil.ClienThread;/* * * author _USER_ */public class ClienFrame extends javax.swing.JFrameprivate ClienThread clienThread;/* Creates new form ClienFrame */public ClienFrame()initComponents();clienThread = new ClienThread(this.jTextArea1);clienThread.start();/* * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ GEN-BEGIN:initComponents/ private void initComponents()jScrollPane1 = new javax.swing.JScrollPane();jTextArea1 = new javax.swing.JTextArea();jScrollPane2 = new javax.swing.JScrollPane();jTextArea2 = new javax.swing.JTextArea();jButton1 = new javax.swing.JButton();jButton2 = new javax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setTitle(u5ba2u6237u7aef);setBackground(new java.awt.Color(153, 153, 255);jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumns(20);jTextArea2.setRows(5);jScrollPane2.setViewportView(jTextArea2);jButton1.setText(u53d6u6d88);jButton1.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton1ActionPerformed(evt););jButton2.setText(u53d1u9001);jButton2.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(java.awt.event.ActionEvent evt)jButton2ActionPerformed(evt););javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane();getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,376, Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addComponent(jButton1).addGap(18, 18,18).addComponent(jButton2).addGap(25, 25,25).addComponent(jScrollPane2,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT_SIZE,376, Short.MAX_VALUE).addContainerGap();layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,240, Short.MAX_VALUE).addGap(18, 18, 18).addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jButton1).addComponent(jButton2).addContainerGap();pack();/ / GEN-END:initComponents/* * 取消 * * param evt */private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)this.jTextArea2.setText();/* * 发送 * * param evt */private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)/ 向服务端发消息String message = this.jTextArea2.getText();if (message != null & !message.equals()clienThread.send(message);/ 清空消息输入框this.jTextArea2.setText();/ 显示到消息接收框if (this.jTextArea1.getText() = null| this.jTextArea1.getText().equals()this.jTextArea1.setText(我说: + message); elsethis.jTextArea1.setText(this.jTextArea1.getText() + n我说:+ message);/* * param args * the command line arguments */public static void main(String args)java.awt.EventQueue.invokeLater(new Runnable()public void run()new ClienFrame().setVisible(true););/ GEN-BEGIN:variables/ Variables declaration - do not modifyprivate javax.swing.JButton jButton1;private javax.swing.JButton jButton2;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JScrollPane jScrollPane2;private javax.swing.JTextArea jTextArea1;private javax.swing.JTextArea jTextArea2;/ End of variables declaration/GEN-END:variables15 / 15
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!