标题:模拟QQ
只看楼主
韩明涛
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-11-18
 问题点数:0 回复次数:1 
模拟QQ
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import *;
import *;

public class chat extends JFrame implements ActionListener
   {
         private JLabel label1,label2;
         private JTextField textfield_ip;
         private JTextArea textarea_receive,textarea_send;
         private JButton button_send;
         private JPanel panel1,panel2,panel3;
         private DatagramPacket dpreceive;
         private DatagramPacket dpsend;
         private DatagramSocket ds;
         private byte[] dbreceive;
         private byte[] dbsend;
         
         
       public chat()
       {
       
         this.panel1=new JPanel();
         this.panel2=new JPanel();
         this.panel3=new JPanel();
         
         this.label1=new JLabel("请输入对方IP地址");
         this.textfield_ip=new JTextField(20);
         this.panel1.add(this.label1);
         this.panel1.add(this.textfield_ip);
         
         this.textarea_receive=new JTextArea(10,30);
         this.textarea_send=new JTextArea(50,30);
         this.panel2.add(this.textarea_receive);
         this.panel2.add(this.textarea_send);
         
         this.button_send=new JButton("发送");
         this.button_send.addActionListener(this);
         
         this.label2=new JLabel("");
         
 
         this.panel3.add(this.button_send);
         this.panel3.add(this.label2);
         
         this.setSize(400,400);
         this.setLayout(new BorderLayout());
         this.add(panel1,BorderLayout.NORTH);
         this.add(panel2,BorderLayout.CENTER);
         this.add(panel3,BorderLayout.SOUTH);
         
         this.setTitle("迷你QQ");
         this.setForeground(Color.blue);
         this.setLocation(300,300);
         this.setDefaultCloseOperation(3);
         this.setVisible(true);
         
         try
            {
                this.ds=new DatagramSocket(2000);
                this.dbreceive=new byte[100];
                
                this.dpreceive=new DatagramPacket(this.dbreceive,dbreceive.length);
                while(true)
                   {
                         for(int i=0;i<this.dbreceive.length;i++)
                            this.dbreceive[i]=0;
                            this.ds.receive(this.dpreceive);
                            
                            String s=new String(this.dpreceive.getData()).trim();
                            this.textarea_receive.append("\n"+s);
                   }
            }
         catch(IOException e){System.out.println();}   
         
       }
public void actionPerformed(ActionEvent e)
     {
         String ip=this.textfield_ip.getText().trim();
         String message=this.textarea_send.getText().trim();
           if(ip.equals("")||message.equals(""))
              {
                   this.label2.setText("IP为空或发送内容为空");
              }
            else
             {
                 try
                    {
                         InetAddress ipdress=InetAddress.getByName(ip);
                         this.textarea_send.setText("");
                         this.textarea_receive.append("\n本人:"+message);
                         String messageto="["+InetAddress.getLocalHost()+"]"+message;
                         this.dbsend=messageto.getBytes();
                         this.dpsend=new DatagramPacket(this.dbsend,dbsend.length,ipdress,2000);
                         this.ds.send(this.dpsend);
                    }
                 catch(UnknownHostException uhe)
                    {
                          this.label2.setText("UnknownHostName");
                    }
                    
                 catch(IOException ioe){}     
             }
        }   
         
         
public static void main(String []args)
        {
             new chat();
        }
         
   }
搜索更多相关主题的帖子: private import java awt 模拟 
2008-01-07 08:06
josean
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-4-4
得分:0 
谢了,研究一下!

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃!
2008-01-07 18:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-196220-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.146935 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved