真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java編寫聊天程序代碼 java編程語言代碼

請幫忙給個java的聊天程序唄

我曾經(jīng)學(xué)JAVA的時候正好做過一個聊天室的課題。把代碼給你吧。不過你這個分給的好少呀。

創(chuàng)新互聯(lián)公司長期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為于田企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站建設(shè),于田網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

import java.net.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*; //添加包

public class ChatTool extends Frame

{

private BufferedReader reader;

private PrintWriter writer;

private Socket socket;

private Panel southPanel = new Panel();

private Panel centerPanel = new Panel();

private Panel northPanel = new Panel();

private GridLayout gridLayout = new GridLayout(2, 2);

private GridLayout gridLayout1 = new GridLayout(1,1);

private GridLayout gridLayout2 = new GridLayout(1,1);

private Button button1 = new Button("Reconnect"); //按鈕1

private Button button2 = new Button("Send"); //按鈕2

private Button button3 = new Button("Clear"); //按鈕3

private TextArea msgShower = new TextArea(); //內(nèi)容顯示器

private TextField sendBox = new TextField(); //發(fā)送信息欄

private TextField nameBox=new TextField("Input your name in room/這里輸入您在聊天室的昵稱"); //用戶名欄

/*================================以上是變量成員的定義==================================*/

ActionListener listener1 = new ActionListener() //按鈕1--重新連接按鈕的事件監(jiān)聽.

{

public void actionPerformed(ActionEvent event)

{

connectToSever();

}

};

ActionListener listener2 = new ActionListener() //按鈕2--發(fā)送按鈕的事件監(jiān)聽.

{

public void actionPerformed(ActionEvent event)

{

String userinput=sendBox.getText();

try

{

writer.println(nameBox.getText()+"說:"+sendBox.getText());

writer.flush();

sendBox.setText("");

}

catch(Exception ie){}

}

};

ActionListener listener3 = new ActionListener() //按鈕3--清除顯示信息按鈕的事件監(jiān)聽.

{

public void actionPerformed(ActionEvent event)

{

//System.exit(0);

msgShower.setText("");

}

};

/*=======================================以上是三個按鈕的監(jiān)聽=======================================================*/

public ChatTool(String title)

{

super(title);

msgShower.setBackground(Color.yellow);

msgShower.setEditable(false); //設(shè)定信息顯示所在文本區(qū)禁止編輯.

northPanel.setLayout(gridLayout2);

centerPanel.setLayout(gridLayout1);

southPanel.setLayout(gridLayout);

northPanel.add(nameBox);

centerPanel.add(msgShower);

southPanel.add(sendBox);

southPanel.add(button1);

southPanel.add(button2);

southPanel.add(button3);

button1.addActionListener(listener1);

button2.addActionListener(listener2);

button3.addActionListener(listener3);

add(northPanel,BorderLayout.NORTH);

add(centerPanel,BorderLayout.CENTER);

add(southPanel,BorderLayout.SOUTH);

addWindowListener(new WindowAdapter() //關(guān)閉床紐扣的事件監(jiān)聽

{

public void windowClosing(WindowEvent event)

{

System.exit(0);

}

});

setSize(420, 300);

setVisible(true);

}

/*========================================以上是GUI設(shè)計==================================================*/

public void showMsg()

{

while(true)

{

try

{

msgShower.append(reader.readLine()+"\n");

}

catch(IOException ie){}

}

}

private void connectToSever()

{

try

{

msgShower.append("Try to connect the Sever...\n嘗試連接服務(wù)器...\n");

socket=new Socket("127.0.0.1",5040);

msgShower.append("\nConnect success,Welcom to our Chatroom.\n 服務(wù)器連接成功!歡迎您進入本聊天室.\n \n -===*===-\n");

reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));

writer=new PrintWriter(socket.getOutputStream());

}

catch(Exception e)

{

msgShower.append("\nSever connect field,please try again.\n連接失敗,請檢查網(wǎng)絡(luò)設(shè)置后重新嘗試.\n \n -===*===-\n");

}

}

public static void main(String args[])

{

ChatTool client=new ChatTool("Bored Chatroom.個人聊天室.");

client.setVisible(true);

client.connectToSever();

client.showMsg();

}

}

急需一個java編程實現(xià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); //滾動窗格

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

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

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

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

}

}

}

java的問題,一個簡單的聊天程序

lz 你好

具體代碼如下:

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("小程序已啟動。");

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 編寫簡單網(wǎng)絡(luò)聊天程序

/**

* 基于UDP協(xié)議的聊天程序

*

* 2007.9.18

* */

//導(dǎo)入包

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

//發(fā)送端口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); //填寫發(fā)送消息

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

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

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

byte [] buf;

//定義DatagramSocket的對象必須進行異常處理

//發(fā)送和接收數(shù)據(jù)報包的套接字

DatagramSocket ds = null;

//=============構(gòu)造函數(shù)=====================

public Chat()

{

CreateInterFace();

//注冊消息框監(jiān)聽器

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

//表示接受數(shù)據(jù)報包

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

//關(guān)閉窗體事件

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();//關(guān)閉ds對象//關(guān)閉數(shù)據(jù)報套接字

}

}

});

}

//界面設(shè)計布局

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默認(rèn)風(fēng)格

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函數(shù)===============================

public static void main(String[]args)

{

new Chat();

}

//================================發(fā)送消息===============================

//消息框回車發(fā)送消息事件

public void actionPerformed(ActionEvent e)

{

//得到文本內(nèi)容

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

//判斷消息框是否為空

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

{

JOptionPane.showMessageDialog(null,"發(fā)送消息不能為空","提示",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("");//清空消息框

//點發(fā)送按鈕發(fā)送消息事件

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 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)閉,此程序自動退出!");

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編寫聊天程序代碼 java編程語言代碼
瀏覽地址:http://weahome.cn/article/hpjgic.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部