转载自网上的即时通讯 稍微修改了一下bug

package test3;
import java.awt.Button;  
import java.awt.Color;  
import java.awt.Font;  
import java.awt.Frame;  
import java.awt.Label;  
import java.awt.TextArea;  
import java.awt.TextField;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.WindowAdapter;  
import java.awt.event.WindowEvent;  
import java.io.IOException;  
import java.net.DatagramPacket;  
import java.net.DatagramSocket;  
import java.net.InetAddress;  
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

 
public class Chat {
	
	
	
	public static void main(String[] args) {  
        new MyFrame();  
  
        new MyEvent();  
  
        Thread thread = new Thread(new MyRecevier());  
        thread.start();  
  
    }  
  
}  
  
class MySend {  
  
    public MySend() throws Exception {  
  
        DatagramSocket dSocket = new DatagramSocket(10000);  
        byte[] buf = ("from:" + InetAddress.getLocalHost().getHostAddress() + "\r\n" + MyEvent.myText)  
                .getBytes();  
  
        System.out.println("收到内容:" + MyEvent.myText);  
  
        DatagramPacket dPacket = new DatagramPacket(buf, buf.length,  
                InetAddress.getByName(MyEvent.myIp), 8888);// 自定义  
  
        dSocket.send(dPacket);  
        dSocket.close();  
  
        // 显示自己发的信息  
        // ----------------本机显示测试------------------//  
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/MM/dd HH:mm");
		String date = simpleDateFormat.format(new Date());
        MyFrame.receField.append(date + "\r\nME:" + MyEvent.myText + "\r\n");  
  
    }  
}  
  
class MyRecevier implements Runnable {  
  
    @SuppressWarnings("resource")
	@Override  
    public void run() {  
  
        DatagramSocket dSocket = null;  
  
        try {  
            dSocket = new DatagramSocket(8888);  
        } catch (SocketException e1) {  
            e1.printStackTrace();  
        }  
        ;  
  
        byte[] buf = new byte[1024];  
  
        DatagramPacket dPacket = new DatagramPacket(buf, buf.length);  
  
        while (true) {  
            try {  
                dSocket.receive(dPacket);  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
  
            String string = new String(dPacket.getData(), 0,  
                    dPacket.getLength());  
  
            // ----------------本机显示测试------------------//  
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/MM/dd HH:mm");
            String date = simpleDateFormat.format(new Date());
            MyFrame.receField.append(date + "\r\n" + string + "\r\n");  
  
        }  
  
  
    }  
}  
  
class MyEvent {  
  
    static String myIp = "";  
    static String myText;  
    String myReceText;  
  
    public MyEvent() {  
  
        // 提交  
        MyFrame.btnPost.addActionListener(new ActionListener() {  
  
            @Override  
            public void actionPerformed(ActionEvent e) {  
  
                myIp = MyFrame.ipField.getText().toString().trim();  
  
                String[] strings = myIp.split("\\.");  
                boolean t = true;  
  
                if (strings.length != 4) {  
                    t = false;  
                    JOptionPane.showMessageDialog(null, "请输入正确的IP地址!");  
                } else {  
                    for (int i = 0; i < strings.length; i++) {  
  
                        // 告知此字符串是否匹配给定的正则表达式。  
                        if (!strings[i]  
                                .matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {  
                            t = false;  
                            JOptionPane.showMessageDialog(null, "带有非数字字符!");  
                            break;  
  
                        } else if (Integer.parseInt(strings[i]) > 255  
                                || Integer.parseInt(strings[i]) < 0) {  
                            t = false;  
                            JOptionPane.showMessageDialog(null, "请输入正确的IP地址!");  
                            break;  
                        }  
  
                    }  
                }  
                if (t) {  
                    myIp = MyFrame.ipField.getText().toString().trim();  
                    System.out.println("目标IP:" + myIp);  
                    JOptionPane.showMessageDialog(null, "提交成功!");  
                }  
            }  
        });  
  
        // 清除  
        MyFrame.btnCancel.addActionListener(new ActionListener() {  
  
            @Override  
            public void actionPerformed(ActionEvent e) {  
  
                MyFrame.inputField.setText(" ");  
  
            }  
        });  
  
        // 发送并清除编辑内容  
        MyFrame.btnSend.addActionListener(new ActionListener() {  
  
            @Override  
            public void actionPerformed(ActionEvent e) {  
  
                myText = MyFrame.inputField.getText().toString().trim();  
  
                if (myIp.equals("")) {  
                    JOptionPane.showMessageDialog(null, "请输入目标IP并提交!");  
                } else if (myText.equals("")) {  
                    JOptionPane.showMessageDialog(null, "请输入内容再发送!");  
  
                } else {  
  
                    System.out.println("发送内容:" + myText);  
                    MyFrame.inputField.setText(" ");  
  
                    try {  
  
                        new MySend();  
  
                    } catch (Exception e1) {  
                        e1.printStackTrace();  
                    }  
                }  
  
            }  
        });  
  
        // 刷新聊天记录  
        MyFrame.btnRecord.addActionListener(new ActionListener() {  
  
            @Override  
            public void actionPerformed(ActionEvent e) {  
  
            }  
        });  
  
    }  
  
}  
  
class MyFrame {  
  
    Frame frame;  
    static Button btnSend, btnRecord, btnCancel, btnPost;  
    static TextArea inputField, recordField, receField;  
    static TextField ipField;  
  
    MyFrame() {  
        frame = new Frame("IP聊天");  
        frame.setSize(432, 500);  
        frame.setLayout(null);  
        frame.setLocation(400, 150);  
        frame.setResizable(false);  
        frame.addWindowListener(new WindowAdapter() {  
            public void windowClosing(WindowEvent e) {  
                System.exit(0);  
            }  
        });  
        frame.setVisible(true);  
        frame.setBackground(Color.gray);  
  
        InputFrame();  
    }  
  
    public void InputFrame() {  
        Label label1 = new Label();  
        Label label2 = new Label("input IP :");  
        label1.setBounds(5, 10, 460, 200);  
        label2.setBounds(8, 465, 80, 30);  
  
        ipField = new TextField();  
        ipField.setBounds(85, 467, 148, 25);  
  
        inputField = new TextArea("", 5, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);  
        recordField = new TextArea("", 5, 10, TextArea.SCROLLBARS_NONE);  
        receField = new TextArea("", 5, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);  
        inputField.setBounds(5, 320, 420, 140);  
        inputField.setBackground(new Color(234,234,234));  
        recordField.setBounds(430, 30, 165, 430);  
        recordField.setBackground(new Color(234,234,234));  
        receField.setBounds(5, 30, 420, 285);  
        receField.setBackground(new Color(234,234,234));  
  
        inputField.setFocusable(true);  
        receField.setFocusable(false);  
        recordField.setFocusable(false);  
  
        ipField.setFont(new Font("Monospaced", Font.ITALIC, 15));  
        inputField.setFont(new Font("Monospaced", Font.ITALIC, 25));  
        receField.setFont(new Font("Monospaced", Font.ITALIC, 18));  
        recordField.setFont(new Font("Monospaced", Font.ITALIC, 15));  
  
        btnCancel = new Button("cancel");  
        btnSend = new Button("send");  
        btnRecord = new Button("Chalked Up");  
        btnPost = new Button("sumbit");  
  
        btnCancel.setBounds(295, 465, 60, 30);  
        btnSend.setBounds(360, 465, 60, 30);  
        btnRecord.setBounds(435, 465, 80, 30);  
        btnPost.setBounds(240, 465, 50, 30);  
  
        frame.add(inputField);  
        frame.add(receField);  
        frame.add(btnCancel);  
        frame.add(btnSend);  
        frame.add(btnPost);  
        frame.add(label1);  
        frame.add(label2);  
        frame.add(ipField);  
  
    }  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值