無為

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

            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編程經驗談
          主站蜘蛛池模板: 稻城县| 威远县| 乌拉特中旗| 大安市| 枣庄市| 屏边| 新密市| 台北市| 盘锦市| 舒城县| 济宁市| 金平| 长阳| 阜宁县| 郴州市| 新邵县| 麻城市| 荥阳市| 黑山县| 健康| 临泽县| 新邵县| 仪征市| 海淀区| 县级市| 灌南县| 乌鲁木齐市| 顺义区| 邓州市| 同心县| 封丘县| 广汉市| 鄂尔多斯市| 屏东市| 宁城县| 遂溪县| 乐业县| 江城| 台东市| 泾源县| 堆龙德庆县|