當前位置:文思屋>社會工作>IT認證>

java編寫即時聊天程式

文思屋 人氣:3.14W

怎樣用java編寫一個即時聊天程式呢?下面本站小編就為大家分享最新的java即時聊天程式編寫方法,一起來看看吧!

java編寫即時聊天程式

import java.awt.*;

import java.awt.event.*;

import java.net.*;

/**

* Sample application using Frame.

*

* @author

* @version 1.00 07/06/13

*/

public class Chatp2pFrame extends Frame {

List dataList=new List(26);

TextField Tfip=new TextField(18);

TextField Tfdata=new TextField(19);

Label Lip=new Label("輸入對方的.IP地址:");

Label Ldata=new Label("輸入聊天的內容:");

DatagramSocket socket=null;

/**

* The constructor.

*/

public Chatp2pFrame() {

try

{

socket=new DatagramSocket(5000);

}

catch(Exception e)

{

e.printStackTrace();

}

Tfip.setSize(150,15);

Tfdata.setSize(150,15);

Tfip.setFocusable(true);

Tfip.setForeground(Color.blue);

Tfdata.setForeground(Color.red);

dataList.setForeground(Color.black);

this.add(dataList,"North");

Panel p1=new Panel();

Panel p2=new Panel();

p1.setLayout(new BorderLayout());

p2.setLayout(new BorderLayout());

this.add(p1,"Center");

this.add(p2,"South");

p1.add(Lip,"West");

p1.add(Ldata,"East");

p2.add(Tfip,"West");

p2.add(Tfdata,"East");

new Thread(new Runnable()

{

public void run()

{

byte[] buf=new byte[1024];

DatagramPacket packet=new DatagramPacket(buf,1024);

while(true)

{

try

{

socket.receive(packet);

dataList.add(new String(buf,0,packet.getLength())+" FROM "+packet.getAddress().getHostAddress()+":"+packet.getPort(),0);

}

catch(Exception e)

{

if(!socket.isClosed())

{

e.printStackTrace();

}

}

}

}

}

).start();

Tfdata.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

try

{

byte[] buf=new byte[1024];

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

DatagramPacket packet=new DatagramPacket(buf,buf.length,

InetAddress.getByName(Tfip.getText()),5000);

socket.send(packet);

dataList.add(new String(buf,0,packet.getLength())+" TO "+packet.getAddress().getHostAddress()+":"+packet.getPort(),0);

}

catch(Exception x)

{

x.printStackTrace();

}

Tfdata.setText("");

}

}

);

MenuBar menuBar = new MenuBar();

Menu menuFile = new Menu();

MenuItem menuFileExit = new MenuItem();

menuFile.setLabel("檔案");

menuFileExit.setLabel("退出");

// Add action listener.for the menu button

menuFileExit.addActionListener

(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

Chatp2pFrame.this.windowClosed();

}

}

);

menuFile.add(menuFileExit);

menuBar.add(menuFile);

setTitle("我的聊天程式");

setMenuBar(menuBar);

setSize(new Dimension(310, 500));

this.setResizable(false);

// Add window listener.

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

Chatp2pFrame.this.windowClosed();

}

}

);

}

/**

* Shutdown procedure when run as an application.

*/

protected void windowClosed() {

// TODO: Check if it is safe to close the application

// Exit application.

socket.close();

System.exit(0);

}

public static void main(String[] args) {

// Create application frame.

Chatp2pFrame frame = new Chatp2pFrame();

// Show frame

frame.setVisible(true);

}

}