java简易界面聊天程序源代码.txt

上传人:b****1 文档编号:231060 上传时间:2022-10-07 格式:TXT 页数:6 大小:6.14KB
下载 相关 举报
java简易界面聊天程序源代码.txt_第1页
第1页 / 共6页
java简易界面聊天程序源代码.txt_第2页
第2页 / 共6页
java简易界面聊天程序源代码.txt_第3页
第3页 / 共6页
java简易界面聊天程序源代码.txt_第4页
第4页 / 共6页
java简易界面聊天程序源代码.txt_第5页
第5页 / 共6页
点击查看更多>>
下载资源
资源描述

java简易界面聊天程序源代码.txt

《java简易界面聊天程序源代码.txt》由会员分享,可在线阅读,更多相关《java简易界面聊天程序源代码.txt(6页珍藏版)》请在冰豆网上搜索。

java简易界面聊天程序源代码.txt

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);

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 外语学习 > 日语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1