无为

无为则可为,无为则至深!

  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 草儿 阅读(153) 评论(0)  编辑  收藏 所属分类: Java编程经验谈

只有注册用户登录后才能发表评论。


网站导航: