当前位置:首页 > 代码 > 正文

java在线聊天程序源代码(在线java编程)

admin 发布:2022-12-19 19:56 145


本篇文章给大家谈谈java在线聊天程序源代码,以及在线java编程对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 聊天室 源代码

【ClientSocketDemo.java 客户端Java源代码】

import java.net.*;

import java.io.*;

public class ClientSocketDemo

{

//声明客户端Socket对象socket

Socket socket = null;

//声明客户器端数据输入输出流

DataInputStream in;

DataOutputStream out;

//声明字符串数组对象response,用于存储从服务器接收到的信息

String response[];

//执行过程中,没有参数时的构造方法,本地服务器在本地,取默认端口10745

public ClientSocketDemo()

{

try

{

//创建客户端socket,服务器地址取本地,端口号为10745

socket = new Socket("localhost",10745);

//创建客户端数据输入输出流,用于对服务器端发送或接收数据

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//获取客户端地址及端口号

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

//向服务器发送数据

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

//从服务器接收数据

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//执行过程中,有一个参数时的构造方法,参数指定服务器地址,取默认端口10745

public ClientSocketDemo(String hostname)

{

try

{

//创建客户端socket,hostname参数指定服务器地址,端口号为10745

socket = new Socket(hostname,10745);

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

//执行过程中,有两个个参数时的构造方法,第一个参数hostname指定服务器地址

//第一个参数serverPort指定服务器端口号

public ClientSocketDemo(String hostname,String serverPort)

{

try

{

socket = new Socket(hostname,Integer.parseInt(serverPort));

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

String ip = String.valueOf(socket.getLocalAddress());

String port = String.valueOf(socket.getLocalPort());

out.writeUTF("Hello Server.This connection is from client.");

out.writeUTF(ip);

out.writeUTF(port);

response = new String[3];

for (int i = 0; i response.length; i++)

{

response[i] = in.readUTF();

System.out.println(response[i]);

}

}

catch(UnknownHostException e){e.printStackTrace();}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

String comd[] = args;

if(comd.length == 0)

{

System.out.println("Use localhost(127.0.0.1) and default port");

ClientSocketDemo demo = new ClientSocketDemo();

}

else if(comd.length == 1)

{

System.out.println("Use default port");

ClientSocketDemo demo = new ClientSocketDemo(args[0]);

}

else if(comd.length == 2)

{

System.out.println("Hostname and port are named by user");

ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);

}

else System.out.println("ERROR");

}

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

【ServerSocketDemo.java 服务器端Java源代码】

import java.net.*;

import java.io.*;

public class ServerSocketDemo

{

//声明ServerSocket类对象

ServerSocket serverSocket;

//声明并初始化服务器端监听端口号常量

public static final int PORT = 10745;

//声明服务器端数据输入输出流

DataInputStream in;

DataOutputStream out;

//声明InetAddress类对象ip,用于获取服务器地址及端口号等信息

InetAddress ip = null;

//声明字符串数组对象request,用于存储从客户端发送来的信息

String request[];

public ServerSocketDemo()

{

request = new String[3]; //初始化字符串数组

try

{

//获取本地服务器地址信息

ip = InetAddress.getLocalHost();

//以PORT为服务端口号,创建serverSocket对象以监听该端口上的连接

serverSocket = new ServerSocket(PORT);

//创建Socket类的对象socket,用于保存连接到服务器的客户端socket对象

Socket socket = serverSocket.accept();

System.out.println("This is server:"+String.valueOf(ip)+PORT);

//创建服务器端数据输入输出流,用于对客户端接收或发送数据

in = new DataInputStream(socket.getInputStream());

out = new DataOutputStream(socket.getOutputStream());

//接收客户端发送来的数据信息,并显示

request[0] = in.readUTF();

request[1] = in.readUTF();

request[2] = in.readUTF();

System.out.println("Received messages form client is:");

System.out.println(request[0]);

System.out.println(request[1]);

System.out.println(request[2]);

//向客户端发送数据

out.writeUTF("Hello client!");

out.writeUTF("Your ip is:"+request[1]);

out.writeUTF("Your port is:"+request[2]);

}

catch(IOException e){e.printStackTrace();}

}

public static void main(String[] args)

{

ServerSocketDemo demo = new ServerSocketDemo();

}

}

急需一个java编程实现的简单聊天窗口代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ClientDemo01 {

public static void main(String[] args){

JFrame f=new JFrame("AA");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(15,30);

ta.setEditable(false); //文本域只读

JScrollPane sp=new JScrollPane(ta); //滚动窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("发送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

socket=new Socket("192.168.0.4",5000);

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread01 mt=new MyThread01(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener01(tf,ta,bos));

}

}

class ButtonActionListener01 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText();

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("AA:"+message+"\n"); //添加到文本域并换行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("发送失败");

}

}

}

}

class MyThread01 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread01(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("BB:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

} import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ServerDemo01{

public static void main(String[] args){

JFrame f=new JFrame("BB");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(12,30); //文本域,第一个参数为行数,第二个参数为列数

ta.setEditable(false); //文本域只读

JScrollPane sp=new JScrollPane(ta); //滚动窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("发送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ServerSocket server=null;

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

server=new ServerSocket(5000);

//ta.append("等待AA连接...\n");

socket=server.accept();

//ta.append("AA已连接\n");

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread1 mt=new MyThread1(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener1(tf,ta,bos));

}

}

class ButtonActionListener1 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText(); //获取文本框中的内容

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("BB:"+message+"\n"); //添加到文本域并换行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("发送失败!");

}

}

}

}

class MyThread1 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread1(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("AA:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

}

要JAVA聊天程序代码,急用

import java.io.InputStream;

import java.io.DataInputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.DataOutputStream;

import java.io.BufferedReader;

import java.net.ServerSocket;

import java.net.Socket;

import java.io.IOException;

import java.util.Date;

class Server

{

public Server()

{

try

{

ServerSocket s=new ServerSocket(8888);

Socket ss=s.accept();

OutputStream out=ss.getOutputStream();

DataOutputStream dout=new DataOutputStream(out);

InputStream in=ss.getInputStream();

DataInputStream din=new DataInputStream(in);

System.out.print(din.readUTF()+"!");

dout.writeUTF("你已经连接到服务器"+"\t"+"你的地址:"+ss.getInetAddress()+"\t"

+"你的链接端口:"+ss.getLocalPort()+"\n");

new ReadMessage(din).start();

new SendMessage(dout).start();

}

catch (IOException e)

{

e.printStackTrace();

}

}

public static void main(String[] args)

{

new Server();

}

}

//接受客户端信息

class ReadMessage extends Thread

{

private DataInputStream din;

public ReadMessage(DataInputStream din)

{

this.din=din;

}

public void run()

{

String str;

try

{

while (true)

{

str=din.readUTF();

System.out.println(new Date().toLocaleString()+"客户端说:"+str);

if (str.equals("bye"))

{

System.out.println("客户端下线!");

break;

}

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

// 发出服务器信息

class SendMessage extends Thread

{

private DataOutputStream dout;

public SendMessage(DataOutputStream dout)

{

this.dout=dout;

}

public void run()

{

InputStreamReader inr=new InputStreamReader(System.in);

BufferedReader buf=new BufferedReader(inr);

String str;

try

{

while(true)

{

str=buf.readLine();

dout.writeUTF(str);

if (str.equals("bye"))

{

System.out.println("服务器退出!");

System.exit(1);

}

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

import java.io.InputStream;

import java.io.DataInputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.DataOutputStream;

import java.io.BufferedReader;

import java.net.Socket;

import java.io.IOException;

import java.util.Date;

class Client

{

public Client()

{

try

{

Socket s=new Socket("192.168.1.2",8888);

InputStream in=s.getInputStream();

DataInputStream din=new DataInputStream(in);

OutputStream out=s.getOutputStream();

DataOutputStream dout=new DataOutputStream(out);

dout.writeUTF("服务器你好!我是客户端");

System.out.println(din.readUTF());

new Thread(new SenderMessage(dout)).start();

new Thread(new ReaderMessage(din)).start();

}

catch (IOException e)

{

e.printStackTrace();

}

}

public static void main(String[] args)

{

new Client();

}

}

class ReaderMessage implements Runnable

{

private DataInputStream din;

public ReaderMessage(DataInputStream din)

{

this.din=din;

}

public void run()

{

String str;

try

{

while(true)

{

str=din.readUTF();

System.out.println(new Date().toLocaleString()+"服务器说:"+str);

if (str.equals("bye"))

{

System.out.println("服务器已经关闭,此程序自动退出!");

break;

}

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

class SenderMessage implements Runnable

{

private DataOutputStream dout;

public SenderMessage(DataOutputStream dout)

{

this.dout=dout;

}

public void run()

{

String str;

InputStreamReader inf=new InputStreamReader(System.in);

BufferedReader buf=new BufferedReader(inf);

try

{

while (true)

{

str=buf.readLine();

dout.writeUTF(str);

if (str.equals("bye"))

{

System.out.println("客户端自己退出!");

System.exit(1);

}

}

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

用JAVA 编写简单网络聊天程序

/**

* 基于UDP协议的聊天程序

*

* 2007.9.18

* */

//导入包

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.net.*;

public class Chat extends JFrame implements ActionListener

{

//广播地址或者对方的地址

public static final String sendIP = "172.18.8.255";

//发送端口9527

public static final int sendPort = 9527;

JPanel p = new JPanel();

List lst = new List(); //消息显示

JTextField txtIP = new JTextField(18); //填写IP地址

JTextField txtMSG = new JTextField(20); //填写发送消息

JLabel lblIP = new JLabel("IP地址:");

JLabel lblMSG = new JLabel("消息:");

JButton btnSend = new JButton("发送");

byte [] buf;

//定义DatagramSocket的对象必须进行异常处理

//发送和接收数据报包的套接字

DatagramSocket ds = null;

//=============构造函数=====================

public Chat()

{

CreateInterFace();

//注册消息框监听器

txtMSG.addActionListener(this);

btnSend.addActionListener(this);

try

{

//端口:9527

ds =new DatagramSocket(sendPort);

}

catch(Exception ex)

{

ex.printStackTrace();

}

//============接受消息============

//匿名类

new Thread(new Runnable()

{

public void run()

{

byte buf[] = new byte[1024];

//表示接受数据报包

while(true)

{

try

{

DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);

ds.receive(dp);

lst.add("【消息来自】◆" + dp.getAddress().getHostAddress() + "◆"+"【说】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);

}

catch(Exception e)

{

if(ds.isClosed())

{

e.printStackTrace();

}

}

}

}

}).start();

//关闭窗体事件

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent w)

{

System.out.println("test");

int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);

if(n==JOptionPane.YES_OPTION)

{

dispose();

System.exit(0);

ds.close();//关闭ds对象//关闭数据报套接字

}

}

});

}

//界面设计布局

public void CreateInterFace()

{

this.add(lst,BorderLayout.CENTER);

this.add(p,BorderLayout.SOUTH);

p.add(lblIP);

p.add(txtIP);

p.add(lblMSG);

p.add(txtMSG);

p.add(btnSend);

txtIP.setText(sendIP);

//背景颜色

lst.setBackground(Color.yellow);

//JAVA默认风格

this.setUndecorated(true);

this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

this.setSize(600,500);

this.setTitle("〓聊天室〓");

this.setResizable(false);//不能改变窗体大小

this.setLocationRelativeTo(null);//窗体居中

this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

this.setVisible(true);

txtMSG.requestFocus();//消息框得到焦点

}

//===============================Main函数===============================

public static void main(String[]args)

{

new Chat();

}

//================================发送消息===============================

//消息框回车发送消息事件

public void actionPerformed(ActionEvent e)

{

//得到文本内容

buf = txtMSG.getText().getBytes();

//判断消息框是否为空

if (txtMSG.getText().length()==0)

{

JOptionPane.showMessageDialog(null,"发送消息不能为空","提示",JOptionPane.WARNING_MESSAGE);

}

else{

try

{

InetAddress address = InetAddress.getByName(sendIP);

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);

ds.send(dp);

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

txtMSG.setText("");//清空消息框

//点发送按钮发送消息事件

if(e.getSource()==btnSend)

{

buf = txtMSG.getText().getBytes();

try

{

DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);

}

catch(Exception ex)

{

ex.printStackTrace();

}

txtMSG.setText("");//清空消息框

txtMSG.requestFocus();

}

}

}

急求一个JAVA编写的局域网聊天代码?不要求很复杂。只要可以在两台机子上简单的对话就可以。

//以下引入包

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import java.applet.*;

public class regit extends JApplet implements ActionListener,ItemListener//接口

{

String s=null;

//对象的声明

JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//标签

JTextField textname,textaddress,textmail,textphone;//文本框

JRadioButton r1,r2;//单选按纽

ButtonGroup bg;//组

JPasswordField textpass;//密码域

JButton buttonregit,buttonreset;//注册按纽 重写按纽

JPanel p;//面板

String sex;//定义性别字符串

URL url;//统一资源定位

BufferedWriter out1,out2;//流

BufferedReader in;

//布局方式

GridBagLayout gbl;

GridBagConstraints gc;

AppletContext co;//接口

//初始化

public void init()

{

//new 对象

labelname=new JLabel("用 户 名:");

labelpass=new JLabel("用户密码:");

labelsxe=new JLabel("性别:");

labeladdress=new JLabel("地址:");

labelmail=new JLabel("电子邮件:");

labelphone=new JLabel("联系电话:");

textname=new JTextField(15); textname.setForeground(Color.red);

textname.setToolTipText("请在这输入你的用户名");

textaddress=new JTextField(15);textaddress.setForeground(Color.red);

textaddress.setToolTipText("请在这输入你的地址");

textmail=new JTextField(15); textmail.setForeground(Color.red);

textmail.setToolTipText("请在这里输入你的E-mail地址");

textphone=new JTextField(15); textphone.setForeground(Color.red);

textphone.setToolTipText("请在这输入你的电话号码");

r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//设置颜色

r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//设置颜色

bg=new ButtonGroup();

bg.add(r1);bg.add(r2);//加入组,实现单选

textpass=new JPasswordField(15);

textpass.setToolTipText("在这里输入密码");

textpass.setForeground(Color.red);

buttonregit=new JButton("注册"); buttonregit.setBackground(new Color(47,177,210));//设置颜色

buttonregit.setToolTipText("点击按纽完成注册");

buttonreset=new JButton("填写"); buttonreset.setBackground(new Color(47,177,210));//设置颜色

buttonreset.setToolTipText("点击按纽刷新重写");

gbl=new GridBagLayout(); ///////////////////////////////////////

gc=new GridBagConstraints(); //////采用GridBagLayout布局方式////////

p=new JPanel();

p.setLayout(gbl);

p.setBackground(new Color(47,177,210));

this.getContentPane().add(p);//加入面板

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=2;

gbl.setConstraints(labelname,gc);

p.add(labelname);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=2;

gbl.setConstraints(textname,gc);

p.add(textname);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=4;

gbl.setConstraints(labelpass,gc);

p.add(labelpass);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=4;

gbl.setConstraints(textpass,gc);

p.add(textpass);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=6;

gbl.setConstraints(labelsxe,gc);

p.add(labelsxe);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=6;

gbl.setConstraints(r1,gc);

p.add(r1);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=5;

gc.gridy=6;

gbl.setConstraints(r2,gc);

p.add(r2);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=8;

gbl.setConstraints(labeladdress,gc);

p.add(labeladdress);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=8;

gbl.setConstraints(textaddress,gc);

p.add(textaddress);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=10;

gbl.setConstraints(labelmail,gc);

p.add(labelmail);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=10;

gbl.setConstraints(textmail,gc);

p.add(textmail);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=12;

gbl.setConstraints(labelphone,gc);

p.add(labelphone);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=12;

gbl.setConstraints(textphone,gc);

p.add(textphone);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=2;

gc.gridy=16;

gbl.setConstraints(buttonregit,gc);

p.add(buttonregit);

gc.anchor=GridBagConstraints.NORTHWEST;

gc.gridx=4;

gc.gridy=16;

gbl.setConstraints(buttonreset,gc);

p.add(buttonreset);

/////////////////////////////////////////////

co=this.getAppletContext();

/////////////////////////////////////////////

buttonregit.addActionListener(this);//按纽事件的监听

buttonreset.addActionListener(this);//按纽事件的监听

r1.addItemListener(this);//选择事件的监听

r2.addItemListener(this);//选择事件的监听

textphone.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

//注册按纽事件

if(e.getSource()==buttonregit)

{

String s1=textname.getText(); ////////////////////

String s2=new String(textpass.getPassword());////////////////////

String s3=textaddress.getText(); // 定义字符串 //

String s4=textmail.getText(); ////////////////////

String s5=textphone.getText(); ////////////////////

//判断注册资料 信息 是否为空

if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)

{

int error=JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(null,"资料不能为空,请重新注册!","【温馨提示】",error);

return;//弹出对话框并返回

}

try//写入到txt文件

{

in=new BufferedReader(new FileReader("d:\\迷离视线聊天室\\password.txt"));

}

catch(Exception ee){}

String ss=s1;

try

{

while((s=in.readLine())!=null)

{

if(s.startsWith(ss))

{

JOptionPane.showMessageDialog(null,"用户名已经存在,请更换名字!");

textname.setText("");//设置为空,重新输入

textpass.setText("");

textaddress.setText("");

textmail.setText("");

textphone.setText("");

return;

}

}

}

catch(Exception ee){}

///////////////////////////以上代码判断是否有同名

{

try

{

out1=new BufferedWriter(new FileWriter("d:\\迷离视线聊天室\\password.txt",true));

out2=new BufferedWriter(new FileWriter("d:\\迷离视线聊天室\\message.txt",true));

}//创建文件

catch(Exception ee)

{}

try

{

out1.write(s1+"#"+s2);//写

out1.newLine();

out2.write("用户名:"+s1);

out2.newLine();

out2.write("密码:"+s2);

out2.newLine();

out2.write("性别:"+sex);

out2.newLine();

out2.write("地址:"+s3);

out2.newLine();

out2.write("电子邮件:"+s4);

out2.newLine();

out2.write("电话:"+s5);

out2.newLine();

out1.flush();

out2.flush();//清理缓冲

out1.close();

out2.close();

}

catch(Exception ee)

{}

JOptionPane.showMessageDialog(null,"注册成功!");

try

{

String qss="";

url=new url(/qss);//连接上网址

co.showDocument(url);

}

catch(Exception exx)

{}

}

}

//////////////////////以下为回车事件

if(e.getSource()==textphone)

{

String s1=textname.getText(); ////////////////////

String s2=new String(textpass.getPassword());////////////////////

String s3=textaddress.getText(); // 定义字符串 //

String s4=textmail.getText(); ////////////////////

String s5=textphone.getText(); ////////////////////

//判断注册资料 信息 是否为空

if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)

{

int error=JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(null,"资料不能为空,请重新注册!","【温馨提示】",error);

return;//弹出对话框并返回

}

try//写入到txt文件

{

in=new BufferedReader(new FileReader("d:\\迷离视线聊天室\\password.txt"));

}

catch(Exception ee){}

String ss=s1;

try

{

while((s=in.readLine())!=null)

{

if(s.startsWith(ss))

{

JOptionPane.showMessageDialog(null,"用户名已经存在,请更换名字!");

textname.setText("");//设置为空,重新输入

textpass.setText("");

textaddress.setText("");

textmail.setText("");

textphone.setText("");

return;

}

}

}

catch(Exception ee){}

///////////////////////////以上代码判断是否有同名

{

try

{

out1=new BufferedWriter(new FileWriter("d:\\迷离视线聊天室\\password.txt",true));

out2=new BufferedWriter(new FileWriter("d:\\迷离视线聊天室\\message.txt",true));

}//创建文件

catch(Exception ee)

{}

try

{

out1.write(s1+"#"+s2);//写

out1.newLine();

out2.write("用户名:"+s1+"密码:"+s2+"性别:"+sex+"地址:"+s3+"电子邮件:"+s4+"电话:"+s5);//写

out2.newLine();

out1.flush();

out2.flush();//清理缓冲

out1.close();

out2.close();

}

catch(Exception ee)

{}

JOptionPane.showMessageDialog(null,"注册成功!");

try

{

String qss="";

url=new url(/qss);//连接上网址

co.showDocument(url);

}

catch(Exception exx)

{}

}

}

if(e.getSource()==buttonreset)//刷新重写事件

{

textname.setText("");

textpass.setText("");

textaddress.setText("");

textmail.setText("");

textphone.setText("");

}

}

//////////////////////////////////////////

//

public void itemStateChanged(ItemEvent ex)

{

if(ex.getSource()==r1)

{

sex=new String("男");

}

else if(ex.getSource()==r2)

{

sex=new String("女");

}

}

}

参考资料:试试看,刚在网上找的

java在线聊天程序源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于在线java编程、java在线聊天程序源代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/21706.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载