五年级上册语文练答案:简单的聊天室程序2

来源:百度文库 编辑:高校问答 时间:2024/04/26 19:18:25
public void actionPerformed(ActionEvent e){
if (e.getSource ()==send)
sendData();
else if (e.getSource ()==clear)
output.setText(" ");
else if (e.getSource ()==about){
AboutQQ test=new AboutQQ (this);
}
}

public static void main(String[] args){
new QQ();
}

void sendData(){
try{
String msg=input.getText();
if(msg.equals(" "))
return;
input.setText(" ");
String ad=IPAdd.getText();
InetAddress tea=InetAddress.getLocalHost();
String asd=tea.getHostAddress(); //发送方的IP地址
output.append("["+asd+"]:("+time.get(GregorianCalendar.YEAR)
+"-"+time.get(GregorianCalendar.MONTH)+"-"
+time.get(GregorianCalendar.DATE)+" "
+time.get(GregorianCalendar.HOUR)+":"
+time.get(GregorianCalendar.MINUTE)+":"
+time.get(GregorianCalendar.SECOND)+") +"\n"+msg
+"\n");
msg="From ["+asd+"]:("+time.get(GregorianCalendar.YEAR)
+"-"+time.get(GregorianCalendar.MONTH)+"-";
+time.get(GregorianCalendar.DATE)+" "
+time.get(GregorianCalendar.HOUR)+":"
+time.get(GregorianCalendar.MINUTE)+":"
+time.get(GregorianCalendar.SECOND)+") +"\n"+msg);

InetAddress address=InetAddress.getByName(ad);
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes (0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
Socket.send(packet);
}catch (Exception e){
}

}

void WaitForData(){
try{
byte[]buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(9999);
while(true){
Socket.receive(packet);
String s=new String (buffer,0,0,packet.getLengrh());
Output.append(s+"\n");
Packet=new DatagramPacket(buffer,buffer.length);
}
}catch(Exception e){
}
}

}

class CloseWin extends WindowAdapter{
public void WindowClosing(WindowEvent e){
Fram fr=(Fram)(e.getSource);
fr.dispose();
System.exit(0);
}
}

class AboutQQ{
private Labe1 Labe1;
private JDialog dialog;

public AboutQQ (Fram f){
Labe1=new Labe1("Version 1.0");
dialog=new JDialog (f,"About",true);
dialog.setLocation(f.getLocation());
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new BorderLayout());
dialogPane.add(labe1);
dialogPane.setBounds(50,50,50,50);
dialog.pack();
dialog.show();
}
}
这是从资料上下的一个java程序,调试后还有问题运行不成功
请高手帮忙!接上一条简单聊天室的程序,谢谢

不大清楚