import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class ChatFrame extends JFrame
{
JPanel contentPane;
Label label1 = new Label(); //发送消息
Label label2 = new Label(); //输入IP
Label label3 = new Label(); //输入端口标签
Label label4 = new Label(); //输出端口标签
TextField textField1 = new TextField(); //输入IP
TextField textField2 = new TextField(); //发送消息
TextField textField3 = new TextField(); //获得输入端口
TextField textField4 = new TextField(); //获得输出端口
Button button1 = new Button(); //发送消息
Button button2 = new Button(); //建立连接
TextArea textArea1 = new TextArea(); //显示信息
boolean online = flase; //连接标志
int portIn = 5000; //输入端口
int portOut = 5001; //输出端口
DatagramPacket sendpacket,receivepacket; //声明发送和接收数据包
DatagramSocket sendsocket,receivesocket; //声明发送和接受数据接口
public ChatFrame(){}
private void toInit() throws Exception {} //初始化窗体
protected void processWindowEvent(WindowEvent e) {} //关闭窗口
public void waitforpackets() {} //接受消息
void button1_actionPerformed(ActionEvent e) {} //发送消息
void buuton2_actionPerformed(ActionEvent e) {} //建立连接
}
private void toInit() throws Exception
{
contentPane = (JPanel)this.getContentPane();
label1.setText("发送消息");
label1.setBounds(new Rectangle(13, 100, 60, 21));
contentPane.setLayout(null);
this.setSize(new Dimension(363, 320));
this.setTitle("二人聊天室");
label2.setText("输入对方IP地址:");
label2.setBounds(new Rectangle(12, 19, 109, 23));
label3.setText("输入端口:");
label3.setBounds(new Rectangle(12, 58, 55, 22));
label4.setText("输出端口:");
label4.setBounds(new Rectangle(180, 58, 55, 22));
textField3.setText("5000");
textField4.setText("5001");
textField2.setText("0.0.0.0");
button1.setLabel("发送");
button1.setBounds(new Rectangle(280, 100, 59, 21));
button1.addActionListener(new java.awt.event.ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
button2.setLabel("连接");
button2.setBounds(new Rectangle(280, 100, 59, 21));
button2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
public void actionPerformed(ActionEvent e)
{
button2_actionPerformed(e);
}
}
});
textField4.setBounds(new Rectangle(240, 58, 90, 22));
textField3.setBounds(new Rectangle(70, 58, 90, 22));
textField2.setBounds(new Rectangle(123, 20, 120, 19));
textField1.setBounds(new Rectangle(76, 100, 190, 19));
textField1.setEditable(flase);
contentPane.add(label4, null);
contentPane.add(label3, null);
contentPane.add(label2, null);
contentPane.add(label1, null);
contentPane.add(textField1, null);
contentPane.add(textField2, null);
contentPane.add(textField3, null);
contentPane.add(textField4, null);
contentPane.add(textArea1, null);
contentPane.add(button1, null);
contentPane.add(button2, null);
try
{
sendsocket = new DatagramSocket(5001); //作为输出端
receivesocket = new DatagramSocket(5000); //作为输入端
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(0);
}
}
void button2_actionPerformed(ActionEvent e)
{
String str2 = new String();
String Str3 = new String();
str2 = textField3.getText();
str3 = textField4.getText();
if(str2.compareTo("")!=0 && str3.compareTo("")!=0)
{
portIn = (int) Integer.parseInt(str2);
portOut = (int) Integer.parseInt(str3);
if(online)
{
sendsocket.close();
receivesocket.close();
textArea1.append("\n formal connection closed");
}
try
{
textArea1.append("\n building a connection use the port defined");
sendcocket = new DatagramSocket(portOut);
receivesocket = new DatagramSocket(portIn);
textArea1.append("\n connection builded.");
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(0);
}
online = sendsocket.isClosed();
}
else
{
textArea1.append("\n please input port and ip");
}
}
public void waitforpackets() //检测接受对方发来的信息
{
while(true)
{
try
{
byte[] Array = new byte[100];
receivepacket = new DatagramPacket(Array,Array.length);
receivesocket.receive(receivepacket);
textArea1.append("\nfrom" + receivepacket.getAddress() + ":" + recivepacket.getPort() + " get:");
byte data[] = receivepacket.getData();
String received = new String (data,0);
textArea1.append(received);
}
catch(IOException se)
{
se.printStackTrace();
}
}
}
void button1_actionPerformed(ActionEvent e)
{
String str = textField2.getText();
if(online)
{
if(str.compareTo("")!=0)
{
try
{
textArea1.append("\nto " + textField2.getText() + ":" + portOut + " say: " + textField1.getText());
String s = TextField1.getText();
byte data[] = new byte[100];
s.getBytes(0, s.leng(), data, 0);
sendpacket = new DatagramPacket(data, s.length(), InetAddress.getByName(textField2.getText()), portOut);
sendsocket.send(sendpacket);
}
catch(IOException exc)
{
exc.printStackTrace();
}
}
}
else
{
textArea1.append("\nplease build a connection first!");
}
}
//第38行有错误,说啥也找不出来了,请大家帮忙!