亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

java編寫即時聊天程序

時間:2020-11-07 16:32:31 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編寫即時聊天程序】相關文章:

1.簡單的Java程序編寫

2.Java怎么編寫走馬燈程序

3.Java程序編寫記事本方法大全

4.如何編譯java程序

5.Java怎么編寫Oracle存儲過程

6.Java程序學習方法

7.Java定時器代碼的編寫

8.實現鼠標畫圖的Java程序