java编写即时聊天程序

时间:2024-08-26 09:46:45 JAVA认证 我要投稿
  • 相关推荐

java编写即时聊天程序

  怎样用java编写一个即时聊天程序呢?下面yjbys小编就为大家分享最新的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);

  }

  }

【java编写即时聊天程序】相关文章:

Java怎么编写走马灯程序08-19

Java怎么编写Oracle存储过程09-15

如何编译java程序09-28

Java编写计算器的的常见做法01-22

Java程序开发与运行环境06-21

实现鼠标画图的Java程序06-22

如何使用Swing编写全屏程序09-20

sun认证java程序员须知Java日志框架09-11

java程序中如何调用linux命令08-27

初级java程序员面试要求01-22