欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

java聊天系統(tǒng)代碼 java聊天程序

java的問題,一個(gè)簡(jiǎn)單的聊天程序

lz 你好

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)壽寧免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

具體代碼如下:

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

public?class?Chat?extends?JFrame{

private?JLabel?enter,start;

private?JTextField?input;

private?JTextArea?output;

public?Chat()?{

super("小程序查看器:Client.class");

enter?=?new?JLabel("html小程序brEnter?Text/html");

enter.setFont(new?Font("宋體",?Font.PLAIN,?12));

start?=?new?JLabel("小程序已啟動(dòng)。");

start.setFont(new?Font("宋體",?Font.PLAIN,?12));

input?=?new?JTextField(30);

output?=?new?JTextArea(10,35);

output.setEditable(false);

input.addKeyListener(new?KeyAdapter(){

public?void?keyPressed(KeyEvent?e){

if(e.getKeyCode()?==?10){

output.append(input.getText()?+?"\n");

}

}

});

setLayout(new?FlowLayout(FlowLayout.LEFT,1,1));

getContentPane().add(enter);

getContentPane().add(input);

getContentPane().add(output);

getContentPane().add(start);

setLocation(300,200);

setSize(450,270);

setResizable(false);

setVisible(true);

setDefaultCloseOperation(3);

}

public?static?void?main(String[]?args)?{

new?Chat();

}

}

希望能幫助你哈

要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("你已經(jīng)連接到服務(wù)器"+"\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();

}

}

}

// 發(fā)出服務(wù)器信息

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("服務(wù)器退出!");

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("服務(wù)器你好!我是客戶端");

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()+"服務(wù)器說:"+str);

if (str.equals("bye"))

{

System.out.println("服務(wù)器已經(jīng)關(guān)閉,此程序自動(dòng)退出!");

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聊天室 客戶端 和 服務(wù)器 完整代碼

CS模式的QQ這是服務(wù)器:ChatServer.javaimport java.net.*;

import java.io.*;

public class ChatServer

{

final static int thePort=8189;

ServerSocket theServer;

ChatHandler[] chatters;

int numbers=0;

public static void main(String args[])

{

ChatServer app=new ChatServer();

app.run();

}

public ChatServer()

{

try

{

theServer=new ServerSocket(thePort);

chatters=new ChatHandler[10];

}

catch(IOException io){}

}

public void run()

{

try

{

System.out.println("服務(wù)器已經(jīng)建立!");

while(numbers10)

{

Socket theSocket=theServer.accept();

ChatHandler chatHandler=new ChatHandler(theSocket,this);

chatters[numbers]=chatHandler;

numbers++;

}

}catch(IOException io){}

}

public synchronized void removeConnectionList(ChatHandler c)

{

int index=0;

for(int i=0;i=numbers-1;i++)

if(chatters[i]==c)index=i;

for(int i=index;inumbers-1;i++)

chatters[i]=chatters[i+1];

chatters[numbers-1]=null;

numbers--;

}

public synchronized String returnUsernameList()

{

String line="";

for(int i=0;i=numbers-1;i++)

line=line+chatters[i].user+":";

return line;

}

public void broadcastMessage(String line)

{

System.out.println("發(fā)布信息:"+line);

for(int i=0;i=numbers-1;i++)

chatters[i].sendMessage(line);

}

}====================================================這是客戶端:ChatClient.javaimport java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ChatClient extends Thread implements ActionListener

{

JTextField messageField,IDField,ipField,portField;

JTextArea message,users;

JButton connect,disconnect;

String user="";

String userList[]=new String[10];

Socket theSocket;

BufferedReader in;

PrintWriter out;

boolean connected=false;

Thread thread;

public static void main(String args[])

{

JFrame frame=new JFrame("聊天室");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ChatClient cc=new ChatClient();

JPanel content=cc.createComponents();

frame.getContentPane().add(content);

frame.setSize(550,310);

frame.setVisible(true);

}

public JPanel createComponents()

{

JPanel pane=new JPanel(new BorderLayout());

message=new JTextArea(10,35);

message.setEditable(false);

JPanel paneMsg=new JPanel();

paneMsg.setBorder(BorderFactory.createTitledBorder("聊天內(nèi)容"));

paneMsg.add(message);

users=new JTextArea(10,10);

JPanel listPanel=new JPanel();

listPanel.setBorder(BorderFactory.createTitledBorder("在線用戶:"));

listPanel.add(users);

messageField=new JTextField(50);

IDField=new JTextField(5);

ipField=new JTextField("LocalHost");

portField=new JTextField("8189");

connect=new JButton("連 接");

disconnect=new JButton("斷 開");

disconnect.setEnabled(false);

JPanel buttonPanel=new JPanel();

buttonPanel.add(new Label("服務(wù)器IP:"));

buttonPanel.add(ipField);

buttonPanel.add(new Label("端口:"));buttonPanel.add(portField);

buttonPanel.add(new Label("用戶名:"));

buttonPanel.add(IDField);

buttonPanel.add(connect);

buttonPanel.add(disconnect);

pane.add(messageField,"South");

pane.add(buttonPanel,"North");

pane.add(paneMsg,"Center");

pane.add(listPanel,"West");

connect.addActionListener(this);

disconnect.addActionListener(this);

messageField.addActionListener(this);

IDField.addActionListener(this);

ipField.addActionListener(this);

return pane;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==connect){

user=IDField.getText();

String ip=ipField.getText();

int port =Integer.parseInt(portField.getText());

if(!user.equals("")connectToServer(ip,port,user))

{

disconnect.setEnabled(true);

connect.setEnabled(false);

}

}

if(e.getSource()==disconnect)disconnectFromServer();

if(e.getSource()==messageField)

if(theSocket!=null)

{

out.println("MESSAGE:"+messageField.getText());

messageField.setText("");

}

}

public void disconnectFromServer()

{

if(theSocket!=null)

{

try

{

connected=false;

out.println("LEAVE:"+user);

disconnect.setEnabled(false);

connect.setEnabled(true);

thread=null;

theSocket.close();

}catch(IOException io){}

theSocket=null;

message.setText("");

users.setText("");

}

}

public boolean connectToServer(String ip,int port,String ID)

{

if(theSocket!=null)

return false;

try

{

theSocket=new Socket(ip,port);

in=new BufferedReader(new InputStreamReader(theSocket.getInputStream()));

out=new PrintWriter(new OutputStreamWriter(theSocket.getOutputStream()),true);

out.println("USER:"+user);

message.setText("");

connected=true;

thread=new Thread(this);

thread.start();

}catch(Exception e){return false;}

return true;

}

public void extractMessage(String line)

{

System.out.println(line);

Message messageline;

messageline=new Message(line);

if(messageline.isValid())

{

if(messageline.getType().equals("JOIN"))

{

user=messageline.getBody();

message.append(user+"進(jìn)入了聊天室\n");

}

else if(messageline.getType().equals("LIST"))

updateList(messageline.getBody());

else if(messageline.getType().equals("MESSAGE"))

message.append(messageline.getBody()+"\n");

else if(messageline.getType().equals("REMOVE"))

message.append(messageline.getBody()+"離開了聊天室\n");

}

else

message.append("出現(xiàn)問題:"+line+"\n");

}

public void updateList(String line)

{

users.setText("");

String str=line;

for(int i=0;i10;i++)

userList[i]="";

int index=str.indexOf(":");

int a=0;

while(index!=-1){

userList[a]=str.substring(0,index);

str=str.substring(index+1);

a++;

index=str.indexOf(":");

}

for(int i=0;i10;i++)

users.append(userList[i]+"\n");

}

public void run(){

try{

String line="";

while(connected line!=null){

line=in.readLine();

if(line!=null) extractMessage(line);

}

}catch(IOException e){}

}

} =======================================================import java.net.*;

import java.io.*;

class ChatHandler extends Thread{

Socket theSocket;

BufferedReader in;

PrintWriter out;

int thePort;

ChatServer parent;

String user="";

boolean disconnect=false;

public ChatHandler(Socket socket,ChatServer parent){

try{

theSocket=socket;

this.parent=parent;

in=new BufferedReader(new InputStreamReader(theSocket.getInputStream()));

out=new PrintWriter(new OutputStreamWriter(theSocket.getOutputStream()),true);

thePort=theSocket.getPort();

start();

}catch(IOException io){}

}

public void sendMessage(String line){

out.println(line);

}

public void setupUserName(String setname){

user=setname;

//System.out.print(user+"參加");

parent.broadcastMessage("JOIN:"+user);

}

public void extractMessage(String line){

Message messageline;

messageline = new Message(line);

if(messageline.isValid()){

if(messageline.getType().equals("USER")){

setupUserName(messageline.getBody());

parent.broadcastMessage("LIST:"+parent.returnUsernameList());

}

else if(messageline.getType().equals("MESSAGE")){

parent.broadcastMessage("MESSAGE:"+user+"說: "+messageline.getBody());

}

else if(messageline.getType().equals("LEAVE")){

String c=disconnectClient();

parent.broadcastMessage("REMOVE:"+c);

parent.broadcastMessage("LIST:"+parent.returnUsernameList());

}

}

else

sendMessage("命令不存在!");

}

public String disconnectClient(){

try{

in.close();

out.close();

theSocket.close();

parent.removeConnectionList(this);

disconnect=true;

}catch(Exception ex){}

return user;

}

public void run(){

String line,name;

boolean valid=false;

try{

while((line=in.readLine())!=null){

System.out.println("收到:"+line);

extractMessage(line);

}

}catch(IOException io){}

}

}

=========================================================

Message.javapublic class Message{

private String type;

private String body;

private boolean valid;

public Message(String messageLine){

valid=false;

type=body=null;

int pos=messageLine.indexOf(":");

if(pos=0){

type=messageLine.substring(0,pos).toUpperCase();

body=messageLine.substring(pos+1);

valid=true;

}

}

public Message(String type,String body){

valid=true;

this.type=type;

this.body=body;

}

public String getType(){

return type;

}

public String getBody(){

return body;

}

public boolean isValid(){

return valid;

}

} ==================================================共有4個(gè)文件,先運(yùn)行服務(wù)段端。。。 這是我以前學(xué)的時(shí)候?qū)戇^的!希望能幫的上你

急求一個(gè)JAVA編寫的局域網(wǎng)聊天代碼?不要求很復(fù)雜。只要可以在兩臺(tái)機(jī)子上簡(jiǎn)單的對(duì)話就可以。

//以下引入包

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;

//對(duì)象的聲明

JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//標(biāo)簽

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

JRadioButton r1,r2;//單選按紐

ButtonGroup bg;//組

JPasswordField textpass;//密碼域

JButton buttonregit,buttonreset;//注冊(cè)按紐 重寫按紐

JPanel p;//面板

String sex;//定義性別字符串

URL url;//統(tǒng)一資源定位

BufferedWriter out1,out2;//流

BufferedReader in;

//布局方式

GridBagLayout gbl;

GridBagConstraints gc;

AppletContext co;//接口

//初始化

public void init()

{

//new 對(duì)象

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

labelpass=new JLabel("用戶密碼:");

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

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

labelmail=new JLabel("電子郵件:");

labelphone=new JLabel("聯(lián)系電話:");

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

textname.setToolTipText("請(qǐng)?jiān)谶@輸入你的用戶名");

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

textaddress.setToolTipText("請(qǐng)?jiān)谶@輸入你的地址");

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

textmail.setToolTipText("請(qǐng)?jiān)谶@里輸入你的E-mail地址");

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

textphone.setToolTipText("請(qǐng)?jiān)谶@輸入你的電話號(hào)碼");

r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//設(shè)置顏色

r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//設(shè)置顏色

bg=new ButtonGroup();

bg.add(r1);bg.add(r2);//加入組,實(shí)現(xiàn)單選

textpass=new JPasswordField(15);

textpass.setToolTipText("在這里輸入密碼");

textpass.setForeground(Color.red);

buttonregit=new JButton("注冊(cè)"); buttonregit.setBackground(new Color(47,177,210));//設(shè)置顏色

buttonregit.setToolTipText("點(diǎn)擊按紐完成注冊(cè)");

buttonreset=new JButton("填寫"); buttonreset.setBackground(new Color(47,177,210));//設(shè)置顏色

buttonreset.setToolTipText("點(diǎn)擊按紐刷新重寫");

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);//按紐事件的監(jiān)聽

buttonreset.addActionListener(this);//按紐事件的監(jiān)聽

r1.addItemListener(this);//選擇事件的監(jiān)聽

r2.addItemListener(this);//選擇事件的監(jiān)聽

textphone.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

//注冊(cè)按紐事件

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

//判斷注冊(cè)資料 信息 是否為空

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

{

int error=JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(null,"資料不能為空,請(qǐng)重新注冊(cè)!","【溫馨提示】",error);

return;//彈出對(duì)話框并返回

}

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,"用戶名已經(jīng)存在,請(qǐng)更換名字!");

textname.setText("");//設(shè)置為空,重新輸入

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

}//創(chuàng)建文件

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,"注冊(cè)成功!");

try

{

String qss="";

url=new url(/qss);//連接上網(wǎng)址

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

//判斷注冊(cè)資料 信息 是否為空

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

{

int error=JOptionPane.INFORMATION_MESSAGE;

JOptionPane.showMessageDialog(null,"資料不能為空,請(qǐng)重新注冊(cè)!","【溫馨提示】",error);

return;//彈出對(duì)話框并返回

}

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,"用戶名已經(jīng)存在,請(qǐng)更換名字!");

textname.setText("");//設(shè)置為空,重新輸入

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

}//創(chuàng)建文件

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,"注冊(cè)成功!");

try

{

String qss="";

url=new url(/qss);//連接上網(wǎng)址

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("女");

}

}

}

參考資料:試試看,剛在網(wǎng)上找的

急需一個(gè)java編程實(shí)現(xiàn)的簡(jiǎn)單聊天窗口代碼

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); //滾動(dòng)窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發(fā)送");

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("發(fā)送失敗");

}

}

}

}

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); //文本域,第一個(gè)參數(shù)為行數(shù),第二個(gè)參數(shù)為列數(shù)

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

JScrollPane sp=new JScrollPane(ta); //滾動(dòng)窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發(fā)送");

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(); //獲取文本框中的內(nèi)容

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

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

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

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("發(fā)送失?。?);

}

}

}

}

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

}

}

}

當(dāng)前標(biāo)題:java聊天系統(tǒng)代碼 java聊天程序
當(dāng)前鏈接:http://aaarwkj.com/article24/dooosje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站小程序開發(fā)、靜態(tài)網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
国产精品亚洲欧美在线| 精品人妻一区二区三区四| 中文字幕久精品视频免费| 久久精品女人天堂av免费观看| 久草免费人妻视频在线| 亚洲欧美精品专区久久| 色综合久久综合香梨网| 人妻口爆视频一区二区三区| 在线不卡日本v二区| 无套内射精品一区二区| 国产农村熟妇av国语对白| 亚洲精品一区av在线观看| 国产一区二区三区日本精品| 欧美 国产 综合 日韩| 91大神九色在线观看| 在线亚洲精品一区二区| 国产91精品激烈高潮白浆| 亚洲青青草原自拍偷拍| 成熟人妻一区二区三区人妻| 中文字幕人妻中文av不卡专区| 丰满人妻中出av在线| 蜜桃视频手机在线观看| 亚洲性图中文字幕在线| 韩国午夜福利视频网站| 首页亚洲一区二区三区| 亚洲中文字幕乱码第一页| 国产毛片精品一区内射| 中文字幕九七精品乱码| 成年爽片在线观看播放欧美| 亚洲成人永久免费精品| 亚洲国产午夜福利在线69| 久久中文人妻丝袜不卡| 久久久精品国产亚洲av日韩| 人妻黄色这里只有精品| 日本一区两区三区不卡视频| 国产亚洲精品美女视频| 不卡的国产在线视频| 午夜国产精品福利一二| 国产精品日本在线观看| 日本中文字幕三级专区| 麻豆专区一区二区三区|