java简单的qq聊天源码.docx
《java简单的qq聊天源码.docx》由会员分享,可在线阅读,更多相关《java简单的qq聊天源码.docx(5页珍藏版)》请在冰点文库上搜索。
java简单的qq聊天源码
GetMessage.java服务端:
import;
import;
import;
import;
import;
import;
import;
import;
publicclassGetMessageextendsThread{
privateinti;
Stringv;
JLabellabel=null;
privateJTextAreatext;
publicGetMessage(inti,JTextAreatext){
this.i=i;
this.text=text;
}
publicvoidrun(){
try{
ServerSocketso=newServerSocket(i);
Sockets=so.accept();
while(true){
InputStreamReaderi=newInputStreamReader(s.getInputStream());
BufferedReaderb=newBufferedReader(i);
v=b.readLine();
text.append("对方说"+v+"\n");
}
}catch(IOExceptione){
//label.setText("对方已经下线");
text.append("对方下线了。
。
。
");
}
}
}
SendMessage.java客户端:
import;
import;
import;
import;
import;
import;
import;
import;
import;
publicclassSendMessageextendsThread{
privateStringip;
privateinti;
Sockets=null;
JLabellabel=null;
JTextFieldtext;
JTextAreatext1;
publicSendMessage(Stringip,inti,JTextAreatext1){
//TODOAuto-generatedconstructorstub
this.ip=ip;
this.i=i;
this.text1=text1;
}
publicvoidrun(){
while(true){
try{
s=newSocket(ip,i);
text1.setText("连接成功"+"\n");
break;
}catch(Exceptione){
try{
Thread.sleep(1000);
}catch(InterruptedExceptione1){
"出错了。
。
。
。
");
}
}
}
}
publicvoidsend(Stringmessage)
{
try{
PrintStreamp=newPrintStream(s.getOutputStream());
p.println(message);
}catch(Exceptione1){
"异常"+e1.getMessage());
}
}
}
Test.java简单的界面和测试类
importjava.awt.*;import;
import;
import;
importjava.util.*;importjavax.swing.*;
import;
classWindowTextAreaextendsJFrameimplementsActionListener
{
Strings;
JTextAreatext1;
JTextAreatext2;
JButtonbutton1,button2,button3;
SendMessaget2;
GetMessaget1;
JLabellable1,lable2;
JTextFieldtext;
WindowTextArea()
{this.s=s;
lable1=newJLabel("对方ip");
text=newJTextField(20);
text1=newJTextArea(6,18);
text2=newJTextArea(6,18);
text2.setEditable(false);
button1=newJButton("发送");
button2=newJButton("关闭");
button3=newJButton("确定ip");
setBounds(100,100,450,300);
setVisible(true);
Containercon=getContentPane();
con.setLayout(newFlowLayout());
con.add(lable1);
con.add(text);
con.add(button3);
con.add(newJScrollPane(text1));
con.add(newJScrollPane(text2));
con.add(button1);
con.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
con.validate();