java模拟qq聊天的源代码.docx
- 文档编号:10151535
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:22
- 大小:18.87KB
java模拟qq聊天的源代码.docx
《java模拟qq聊天的源代码.docx》由会员分享,可在线阅读,更多相关《java模拟qq聊天的源代码.docx(22页珍藏版)》请在冰豆网上搜索。
java模拟qq聊天的源代码
客户端:
packagemutil;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
import.Socket;
importjavax.swing.JTextArea;
publicclassClienThreadextendsThread
{
privateSocketsocket=null;
privatejavax.swing.JTextAreajTextArea1;
publicClienThread(JTextAreajTextArea1)
{
this.jTextArea1=jTextArea1;
}
publicvoidrun()
{
try
{
//创建连接
socket=newSocket("",12344);
Stringmessage=null;
while(true)
{
//接受服务端请求
message=receive();
//显示到消息接收框
if(message!
=null&&!
message.equals(""))
{
if(this.jTextArea1.getText()==null
||this.jTextArea1.getText().equals(""))
{
this.jTextArea1.setText("服务端:
"+message);
}else
{
this.jTextArea1.setText(this.jTextArea1.getText()
+"\n服务端:
"+message);
}
}else
{
message="服务端以断开连接或未连接!
";
if(this.jTextArea1.getText()==null
||this.jTextArea1.getText().equals(""))
{
this.jTextArea1.setText("服务端:
"+message);
}else
{
this.jTextArea1.setText(this.jTextArea1.getText()
+"\n服务端:
"+message);
}
break;
}
}
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*向服务端发送消息
*/
publicvoidsend(Stringmessage)
{
try
{
PrintWriterprintWriter=newPrintWriter(socket.getOutputStream());
printWriter.write(message+"\n");
printWriter.flush();
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*接受服务端消息
*/
publicStringreceive()
{
Stringmessage="";
try
{
BufferedReaderbufferedReader=newBufferedReader(
newInputStreamReader(socket.getInputStream()));
message=bufferedReader.readLine();
}catch(IOExceptione)
{
System.out.println("服务端已断开或未连接!
");
}
returnmessage;
}
}
服务端:
packagemutil;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
import.ServerSocket;
import.Socket;
importjavax.swing.JTextArea;
publicclassServerThreadextendsThread
{
privateSocketsocket=null;
privateServerSocketserverSocket=null;
privatejavax.swing.JTextAreajTextArea1;
publicServerThread(JTextAreajTextArea1)
{
this.jTextArea1=jTextArea1;
}
publicvoidrun()
{
try
{
//创建连接
serverSocket=newServerSocket(12344);
//监听客户连接
socket=serverSocket.accept();
Stringmessage=null;
while(true)
{
//接受客户请求
message=receive(socket);
if(message!
=null&&!
message.equals(""))
{
//显示到消息接收框
if(this.jTextArea1.getText()==null
||this.jTextArea1.getText().equals(""))
{
this.jTextArea1.setText("客户端:
"+message);
}else
{
this.jTextArea1.setText(this.jTextArea1.getText()
+"\n客户端:
"+message);
}
}else
{
message="客户端以断开连接或未连接!
";
if(this.jTextArea1.getText()==null
||this.jTextArea1.getText().equals(""))
{
this.jTextArea1.setText("客户端:
"+message);
}else
{
this.jTextArea1.setText(this.jTextArea1.getText()
+"\n客户端:
"+message);
}
break;
}
}
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*向客户端发送消息
*/
publicvoidsend(Stringmessage)
{
try
{
PrintWriterprintWriter=newPrintWriter(socket.getOutputStream());
printWriter.write(message+"\n");
printWriter.flush();
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
/**
*接受客户端消息
*/
publicStringreceive(Socketsocket)
{
Stringmessage="";
try
{
BufferedReaderbufferedReader=newBufferedReader(
newInputStreamReader(socket.getInputStream()));
message=bufferedReader.readLine();
}catch(IOExceptione)
{
System.out.println("服务端已断开或未连接!
");
}
returnmessage;
}
}
服务端界面:
/*
*SereveFrame.java
*
*Createdon__DATE__,__TIME__
*/
packagecom.insigma.frame;
importmutil.ServerThread;
/**
*
*@author__USER__
*/
publicclassServerFrameextendsjavax.swing.JFrame
{
privateServerThreadserverThread;
/**CreatesnewformSereveFrame*/
publicServerFrame()
{
initComponents();
serverThread=newServerThread(this.jTextArea1);
serverThread.start();
}
/**
*Thismethodiscalledfromwithintheconstructortoinitializetheform.
*WARNING:
DoNOTmodifythiscode.Thecontentofthismethodisalways
*regeneratedbytheFormEditor.
*/
//GEN-BEGIN:
initComponents
//
privatevoidinitComponents()
{
jScrollPane1=newjavax.swing.JScrollPane();
jTextArea1=newjavax.swing.JTextArea();
jScrollPane2=newjavax.swing.JScrollPane();
jTextArea2=newjavax.swing.JTextArea();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("\u670d\u52a1\u7aef");
setBackground(newjava.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("\u53d6\u6d88");
jButton1.addActionListener(newjava.awt.event.ActionListener()
{
publicvoidactionPerformed(java.awt.event.ActionEventevt)
{
jButton1ActionPerformed(evt);
}
});
jButton2.setText("\u53d1\u9001");
jButton2.addActionListener(newjava.awt.event.ActionListener()
{
publicvoidactionPerformed(java.awt.event.ActionEventevt)
{
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayoutlayout=newjavax.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
/**
*取消
*
*@paramevt
*/
privatevoidjButton1ActionPerformed(java.awt.event.ActionEventevt)
{
this.jTextArea2.setText("");
}
/**
*发送
*
*@paramevt
*/
privatevoidjButton2ActionPerformed(java.awt.event.ActionEventevt)
{
//向客户端发消息
Stringmessage=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);
}else
{
this.jTextArea1.setText(this.jTextArea1.getText()+"\n我说:
"
+message);
}
}
}
/**
*@paramargs
*thecommandlinearguments
*/
publicstaticvoidmain(Stringargs[])
{
java.awt.EventQueue.invokeLater(newRunnable()
{
publicvoidrun()
{
newServerFrame().setVisible(true);
}
});
}
//GEN-BEGIN:
variables
//Variablesdeclaration-donotmodify
privatejavax.swing.JButtonjButton1;
privatejavax.swing.JButtonjButton2;
privatejavax.swing.JScrollPanejScrollPane1;
privatejavax.swing.JScrollPanejScrollPane2;
privatejavax.swing.JTextAreajTextArea1;
privatejavax.swing.JTextAreajTextArea2;
//Endofvariablesdeclaration//GEN-END:
variables
}
客户端界面:
/*
*ClienFra
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 模拟 qq 聊天 源代码