java简易界面聊天程序源代码.txt
《java简易界面聊天程序源代码.txt》由会员分享,可在线阅读,更多相关《java简易界面聊天程序源代码.txt(6页珍藏版)》请在冰豆网上搜索。
importjava.io.*;
import.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
/**
*�û�����ClientUI
*/
publicclassClientUIextendsJFrame{
JTextAreamainArea;
JTextAreasendArea;
ChatClientclient;
JTextFieldipArea;
JButtonbtnLink;
publicvoidsetClient(ChatClientclient){
this.client=client;
}
publicClientUI(){
super("�ͻ���");
Containercontain=getContentPane();
contain.setLayout(newBorderLayout());
mainArea=newJTextArea();
JScrollPanemainAreaP=newJScrollPane(mainArea);//Ϊ�ı�����ӹ�����
JPanelpanel=newJPanel();
panel.setLayout(newBorderLayout());
sendArea=newJTextArea(3,8);
JButtonsendBtn=newJButton("����");
sendBtn.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
client.sendMsg(sendArea.getText());
mainArea.append("���ͻ��ˡ�"+sendArea.getText()+"\n");
sendArea.setText("");
}
});
JPanelipPanel=newJPanel();
ipPanel.setLayout(newFlowLayout(FlowLayout.LEFT,10,10));
ipPanel.add(newJLabel("��������"));
ipArea=newJTextField(12);
ipArea.setText("127.0.0.1");
ipPanel.add(ipArea);
btnLink=newJButton("����");
ipPanel.add(btnLink);
btnLink.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
client=newChatClient(ipArea.getText(),6666,ClientUI.this);//����socket����
ClientUI.this.setClient(client);
}
});
panel.add(sendBtn,BorderLayout.EAST);
panel.add(sendArea,BorderLayout.CENTER);
contain.add(ipPanel,BorderLayout.NORTH);
contain.add(mainAreaP,BorderLayout.CENTER);
contain.add(panel,BorderLayout.SOUTH);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
ClientUIui=newClientUI();
}
}
/**
*ͨѶ��ChatClient�����غ����ݵ���
*/
classChatClientextendsThread{
Socketsc;//����sc���������������
BufferedReaderin;//���������������������ڴ洢��������������Ϣ
PrintWriterout;//������ӡ�������������Ϣ�ķ���
ClientUIui;
publicChatClient(Stringip,intport,ClientUIui){//��ʼ��ChatClient��
this.ui=ui;
try{
sc=newSocket(ip,port);//����sc,�÷�����ip�Ͷ˿�������
System.out.println("��˳�����ӵ���������");
out=newPrintWriter(sc.getOutputStream(),true);
in=newBufferedReader(newInputStreamReader(sc.getInputStream()));
}catch(Exceptione){
System.out.println(e);
}
start();
}
publicvoidrun(){//���ڼ����������˷���������Ϣ
Stringmsg="";
while(true){
try{
msg=in.readLine();//�ӻ���������һ���ַ�����msg
}catch(SocketExceptionex){
System.out.println(ex);
break;
}catch(Exceptionex){
System.out.println(ex);
}
if(msg!
=null&&msg.trim()!
=""){//��msg��Ϣ��Ϊ��
System.out.println(">>"+msg);
ui.mainArea.append(msg+"\n");//��msg��Ϣ��ӵ��ͻ��˵��ı�������
}
}
}
publicvoidsendMsg(Stringmsg){//���ڷ�����Ϣ
try{
out.println("���ͻ��ˡ�"+msg);
}catch(Exceptione){
System.out.println(e);
}
}
}
importjava.io.*;
import.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
/**
*����˽���ServerUI
*/
publicclassServerUIextendsJFrame{
JTextAreamainArea;
JTextAreasendArea;
JTextFieldindexArea;
SvrComserver;
publicvoidsetServer(SvrComserver){
this.server=server;
}
publicServerUI(){
super("��������");
Containercontain=getContentPane();
contain.setLayout(newBorderLayout());
mainArea=newJTextArea();
JScrollPanemainAreaP=newJScrollPane(mainArea);
JPanelpanel=newJPanel();
panel.setLayout(newBorderLayout());
sendArea=newJTextArea(3,8);
JButtonsendBtn=newJButton("����");
sendBtn.addActionListener(newActionListener()//ע�ᶯ��������
{
publicvoidactionPerformed(ActionEventae){
server.sendMsg(sendArea.getText());//����Ϣ���ݵ��ͻ���
mainArea.append("����������"+sendArea.getText()+"\n");//����Ϣ��ʾ�ڷ������������¼����
sendArea.setText("");
}
});
JPaneltmpPanel=newJPanel();
indexArea=newJTextField
(2);
indexArea.setText("0");
tmpPanel.add(sendBtn);
tmpPanel.add(indexArea);
panel.add(tmpPanel,BorderLayout.EAST);
panel.add(sendArea,BorderLayout.CENTER);
contain.add(mainAreaP,BorderLayout.CENTER);
contain.add(panel,BorderLayout.SOUTH);
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
ServerUIui=newServerUI();
SvrComserver=newSvrCom(ui);//�������������ͨѶ�̣߳������ܿͻ������ݰ�
}
}
/**
*ͨѶ��SvrCom�����غ����ݵ���
*/
classSvrComextendsThread//����ͨѶ��
{
Socketclient;
ServerSocketsoc;
BufferedReaderin;
PrintWriterout;
ServerUIui;
//ChatServerserver;
publicSvrCom(ServerUIui){//��ʼ��SvrCom��
this.ui=ui;
ui.setServer(this);
try{
soc=newServerSocket(6666);//����������˿�6666
System.out.println("����������ɹ����ȴ��˿ںţ�6666");
client=soc.accept();//���ͻ�����������ʱ������һ������
System.out.println("���ӳɹ�������"+client.toString());
in=newBufferedReader(newInputStreamReader(client
.getInputStream()));
out=newPrintWriter(client.getOutputStream(),true);
}catch(Exceptionex){
System.out.println(ex);
}
start();
}
publicvoidrun(){//���ڼ����ͻ��˷���������Ϣ
Stringmsg="";
while(true){
try{
msg=in.readLine();//��in�����϶�������Ϣ
}catch(SocketExceptionex){
System.out.println(ex);
break;
}catch(Exceptionex){
System.out.println(ex);
}
if(msg!
=null&&msg.trim()!
=""){
System.out.println(">>"+msg);
ui.mainArea.append(msg+"\n");
}
}
}
publicvoidsendMsg(Stringmsg){//���ڷ�����Ϣ
try{
out.println("����������"+msg);//����Ϣд�������
}catch(Exceptione){
System.out.println(e);
}
}
}