日期:2014-05-20 浏览次数:20694 次
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.util.regex.*;;
public class PointToPointUDPChat {
Frame f = new Frame("聊天室");
Label lbRemoteIP = new Label("目标IP");// 对方IP
Label lbRemotePort = new Label("目标端口");
Label lbLocalSendPort = new Label("本地发送端口");
Label lbLocalReceivePort = new Label("本地接收端口");
TextField tfRemoteIP = new TextField(15);// 要发送数据的目标IP
TextField tfRemotePort = new TextField(15);// 要发送数据的目标端口
TextField tfLocalSendPort = new TextField(15);// 使用此端口发送数据
TextField tfLocalReceivePort = new TextField(15);// 使用此端口发送数据
String remoteIP = null;
int remotePort = 0;
int localSendPort = 0;
int localReceivePort = 0;
TextArea allChatContent = new TextArea();
TextField sendChatContent = new TextField(20);
Button connect = new Button("连接");
Button disConnect = new Button("断开");
Button bt = new Button("发送");
Thread receiveThread = null;// 利用start和stop控制是否接收消息
public PointToPointUDPChat() {
initFrame();
}
public static void main(String args[]) {
new PointToPointUDPChat();
}
// 外观布局,添加事件响应
public void initFrame() {// //////
f.setSize(400, 500);// 设置容器的大小
f.setLayout(new BorderLayout());
Panel p = new Panel();
Panel p2 = new Panel();
// 添加组件,布置布局
p.setLayout(new GridLayout(5, 5));
p.add(lbRemoteIP);
p.add(tfRemoteIP);
p.add(lbRemotePort);
p.add(tfRemotePort);
p.add(lbLocalSendPort);
p.add(tfLocalSendPort);
p.add(lbLocalReceivePort);
p.add(tfLocalReceivePort);
p.add(connect);
p.add(disConnect);
f.add("North", p);
connect.addActionListener(new ActionListener() {// 连接按钮事件
public void actionPerformed(ActionEvent e) {
try {