qq聊天

java 代码
 
  1. import java.net.*;  
  2. import java.awt.*;  
  3. import javax.swing.*;  
  4. import java.awt.event.*;  
  5. import java.util.GregorianCalendar;  
  6. import javax.swing.JDialog;  
  7.   
  8. public class QQ extends Frame implements ActionListener {  
  9.     Label label1 = new Label("请输入您要发送的信息(限英文):");  
  10.     Label label2 = new Label("以下是你收到的消息记录:");  
  11.     Label label3 = new Label("把以上消息发给如下IP地址:");  
  12.     TextArea input = new TextArea(""714, TextArea.SCROLLBARS_BOTH);  
  13.     TextArea output = new TextArea(""814, TextArea.SCROLLBARS_BOTH);  
  14.     TextField IPAdd = new TextField("192.168.1.88");  
  15.     Button send = new Button("发送消息");  
  16.     Button about = new Button("关于");  
  17.     Button clear = new Button("清空消息纪录");  
  18.     GregorianCalendar time = new GregorianCalendar();  
  19.   
  20.     QQ() {  
  21.         super("仿QQ聊天工具");  
  22.         setLayout(null);  
  23.         setLocation(250250);  
  24.         this.setSize(518218);  
  25.         this.setResizable(false); // 大小不可变  
  26.         this.setBackground(new Color(220220220));  
  27.         Toolkit kit = Toolkit.getDefaultToolkit();  
  28.         Image myImage = kit.getImage("icons\\QQ.bmp");  
  29.         this.setIconImage(myImage);  
  30.   
  31.         label1.setFont(new Font("宋体", Font.PLAIN, 12));  
  32.         label1.setForeground(new Color(00192));  
  33.         label1.setBounds(82821616);  
  34.   
  35.         input.setBackground(new Color(255255128));  
  36.         input.setFont(new Font("Times New Roman", Font.BOLD, 15));  
  37.         input.setForeground(Color.magenta);  
  38.         input.setBounds(844248120);  
  39.   
  40.         output.setBackground(new Color(128255255));  
  41.         output.setFont(new Font("Times New Roman", Font.PLAIN, 12));  
  42.         output.setForeground(Color.magenta);  
  43.         output.setBounds(26444248136);  
  44.         output.setEditable(false);  
  45.   
  46.         send.setFont(new Font("新宋体", Font.PLAIN, 12));  
  47.         send.setLocation(136188);  
  48.         send.setSize(12022);  
  49.   
  50.         clear.setFont(new Font("新宋体", Font.PLAIN, 12));  
  51.         clear.setLocation(392188);  
  52.         clear.setSize(12022);  
  53.   
  54.         label2.setFont(new Font("宋体", Font.PLAIN, 12));  
  55.         label2.setForeground(new Color(00192));  
  56.         label2.setBounds(2642821616);  
  57.   
  58.         about.setFont(new Font("新宋体", Font.PLAIN, 12));  
  59.         about.setLocation(264188);  
  60.         about.setSize(12022);  
  61.   
  62.         label3.setFont(new Font("宋体", Font.PLAIN, 12));  
  63.         label3.setForeground(new Color(00192));  
  64.         label3.setBounds(817216016);  
  65.   
  66.         IPAdd.setFont(new Font("新宋体", Font.PLAIN, 12));  
  67.         IPAdd.setLocation(8190);  
  68.         IPAdd.setSize(12019);  
  69.   
  70.         add(label1);  
  71.         add(input);  
  72.         add(label3);  
  73.         add(label2);  
  74.         add(output);  
  75.         add(IPAdd);  
  76.         add(send);  
  77.         add(about);  
  78.         add(clear);  
  79.         addWindowListener(new closeWin());  
  80.         send.addActionListener(this);  
  81.         about.addActionListener(this);  
  82.         clear.addActionListener(this);  
  83.   
  84.         show();  
  85.         waitForData();  
  86.     }  
  87.   
  88.     public void actionPerformed(ActionEvent e) {  
  89.         if (e.getSource() == send)  
  90.             sendData();  
  91.         else if (e.getSource() == clear)  
  92.             output.setText("");  
  93.         else if (e.getSource() == about) {  
  94.             AboutQQ test = new AboutQQ(this);  
  95.         }  
  96.     }  
  97.   
  98.     public static void main(String args[]) {  
  99.         new QQ();  
  100.     }  
  101.   
  102.     void sendData() {  
  103.         try {  
  104.             String msg = input.getText();  
  105.             if (msg.equals(""))  
  106.                 return;  
  107.             input.setText("");  
  108.             String ad = IPAdd.getText();  
  109.             InetAddress tea = InetAddress.getLocalHost();  
  110.             String asd = tea.getHostAddress();//发送方的IP地址  
  111.             output.append("[" + asd + "]:(" + time.get(GregorianCalendar.YEAR)  
  112.                     + "-" + time.get(GregorianCalendar.MONTH) + "-"  
  113.                     + time.get(GregorianCalendar.DATE) + " "  
  114.                     + time.get(GregorianCalendar.HOUR) + ":"  
  115.                     + time.get(GregorianCalendar.MINUTE) + ":"  
  116.                     + time.get(GregorianCalendar.SECOND) + ") " + "\n" + msg  
  117.                     + "\n");  
  118.             msg = "From [" + asd + "]:(" + time.get(GregorianCalendar.YEAR)  
  119.                     + "-" + time.get(GregorianCalendar.MONTH) + "-"  
  120.                     + time.get(GregorianCalendar.DATE) + " "  
  121.                     + time.get(GregorianCalendar.HOUR) + ":"  
  122.                     + time.get(GregorianCalendar.MINUTE) + ":"  
  123.                     + time.get(GregorianCalendar.SECOND) + ") \n" + msg;  
  124.             InetAddress address = InetAddress.getByName(ad);  
  125.             int len = msg.length();  
  126.             byte[] message = new byte[len];  
  127.             msg.getBytes(0, len, message, 0);  
  128.             DatagramPacket packet = new DatagramPacket(message, len, address,  
  129.                     9999);  
  130.             DatagramSocket socket = new DatagramSocket();  
  131.             socket.send(packet);  
  132.         } catch (Exception e) {  
  133.         }  
  134.     }  
  135.   
  136.     void waitForData() {  
  137.         try {  
  138.             byte[] buffer = new byte[1024];  
  139.             DatagramPacket packet = new DatagramPacket(buffer, buffer.length);  
  140.             DatagramSocket socket = new DatagramSocket(9999);  
  141.             while (true) {  
  142.                 socket.receive(packet);  
  143.                 String s = new String(buffer, 00, packet.getLength());  
  144.                 output.append(s + "\n");  
  145.                 packet = new DatagramPacket(buffer, buffer.length);  
  146.             }  
  147.         } catch (Exception e) {  
  148.         }  
  149.     }  
  150. }  
  151.   
  152. class closeWin extends WindowAdapter {  
  153.     public void windowClosing(WindowEvent e) {  
  154.         Frame fr = (Frame) (e.getSource());  
  155.         fr.dispose();  
  156.         System.exit(0);  
  157.     }  
  158. }  
  159.   
  160. class AboutQQ {  
  161.     private Label label;  
  162.     private JDialog dialog;  
  163.       
  164.     public AboutQQ(Frame f){  
  165.         label = new Label("Version 1.0");  
  166.         dialog = new JDialog(f, "About"true);  
  167.         dialog.setLocation(f.getLocation());  
  168.         Container dialogPane = dialog.getContentPane();  
  169.         dialogPane.setLayout(new BorderLayout());  
  170.         dialogPane.add(label);  
  171.         dialogPane.setBounds(50,50,50,50);  
  172.         dialog.pack();  
  173.         dialog.show();  
  174.     }  
  175. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值