無為

          無為則可為,無為則至深!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
          /**
           *
           *  Chat.java
           *  一個基于UDP數據廣播的局域網絡會議程序
           * 
           *  作者:qyjohn@SMTH
           *
           */

          import java.awt.*;
          import java.awt.event.*;
          import java.io.*;
          import java.net.*;
          import java.security.*;


          public class Chat extends Frame implements ActionListener 
          {
                  
              TextField OutMsg;
              TextArea  InMsg;
              Broadcast Device;
              
              String      HostName  = "Unknown Host: ";
                  
                  /**
                   *
                   * Constructor
                   *
                   */

                  public Chat()
                  {
                  add(CreateGui());
                  addWindowListener(new WindowAdapter() 
                  {
                      public void windowClosing(WindowEvent e) 
                      {
                           System.exit(0);
                       }
                  });
                  
                  try 
                  {
                          InetAddress LocalHost = InetAddress.getLocalHost();
                          HostName = LocalHost.getHostName() + ": ";
                  } catch (UnknownHostException e) {}
                  
                  Device = new Broadcast();
                  Receiver Recv = new Receiver(Device, InMsg);
                  Recv.start();
                  }
                  
                  /**
                   *
                   * Create GUI.
                   *
                   */
                   
                  public Panel CreateGui()
                  {
                          Panel GuiPanel = new Panel();
                          Panel SendMsg  = new Panel();
                           
                  Button SendButton = new Button("Send Message");
                  SendButton.setActionCommand("1");
                  SendButton.addActionListener(this);
                  
                  OutMsg = new TextField(40);
                  InMsg  = new TextArea(15, 60);
                  
                  SendMsg.setLayout(new GridLayout(1,2));         
                  SendMsg.add(SendButton);
                  SendMsg.add(OutMsg);

                  GuiPanel.setLayout(new GridLayout(2,1));
                  GuiPanel.add(SendMsg);
                  GuiPanel.add(InMsg);
                                   
                  return GuiPanel;
                  }
                          
                  /**
                   *
                   * Action processing method.
                   *
                   * @param e Action event.
                   *
                   */
                   
                  public void actionPerformed(ActionEvent e) 
                  {
                      String command = e.getActionCommand();
                      int OutMsgID = 0;
                      String TextMsg;
                   
                  if (command == "1") 
                  { 
                          TextMsg = HostName + OutMsg.getText();
                          Device.SendData(TextMsg);
                  } 
              }

              
              /**
               *
               * Main method for porgram testing.
               *
               */
               
              public static void main(String[] args) 
              {
                  Chat window = new Chat();

                  window.setTitle("UDP Chat");
                  window.pack();
                  window.setVisible(true);
              }    
          }               


          凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
          、轉載請注明來處和原文作者。非常感謝。

          posted on 2005-12-14 13:21 草兒 閱讀(156) 評論(0)  編輯  收藏 所屬分類: Java編程經驗談
          主站蜘蛛池模板: 郁南县| 聂拉木县| 宣威市| 北票市| 新绛县| 盖州市| 西充县| 黄梅县| 辽阳县| 灵川县| 济阳县| 宁德市| 呼图壁县| 临澧县| 康平县| 炉霍县| 福鼎市| 昌邑市| 和平县| 得荣县| 镇远县| 潮州市| 沾益县| 云浮市| 乐昌市| 郑州市| 广饶县| 赤峰市| 旬邑县| 大埔县| 乡宁县| 西贡区| 博爱县| 许昌县| 临漳县| 东乡县| 谢通门县| 赣榆县| 博罗县| 湾仔区| 内丘县|