1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. java編寫(xiě)即時(shí)聊天程序

        時(shí)間:2020-11-07 16:32:31 JAVA認(rèn)證 我要投稿

        java編寫(xiě)即時(shí)聊天程序

          怎樣用java編寫(xiě)一個(gè)即時(shí)聊天程序呢?下面yjbys小編就為大家分享最新的java即時(shí)聊天程序編寫(xiě)方法,一起來(lái)看看吧!

          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("輸入對(duì)方的.IP地址:");

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

          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編寫(xiě)即時(shí)聊天程序】相關(guān)文章:

        1.簡(jiǎn)單的Java程序編寫(xiě)

        2.Java怎么編寫(xiě)走馬燈程序

        3.Java程序編寫(xiě)記事本方法大全

        4.如何編譯java程序

        5.Java怎么編寫(xiě)Oracle存儲(chǔ)過(guò)程

        6.Java程序?qū)W習(xí)方法

        7.Java定時(shí)器代碼的編寫(xiě)

        8.實(shí)現(xiàn)鼠標(biāo)畫(huà)圖的Java程序

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>