📄 java chat applet -- client.txt
字号:
发信人: jallon (javaFan), 信区: JAVA
标 题: 源码公布1
发信站: 华南网木棉站 (Sun Nov 30 19:49:04 1997), 转信
客户端Java Applet:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public final class JavaChatApplet extends Applet
{
private Label l1,l2;
private Panel p1,p2;
private TextField tf;
private TextArea ta;
private DatagramPacket sendPacket,receivePacket;
private DatagramSocket sendSocket,receiveSocket;
public void init()
{
setBackground(Color.yellow);
setLayout(new BorderLayout());
p1=new Panel();
p1.setLayout(new BorderLayout());
l1=new Label("Our chatting as follows:");
ta=new TextArea(10,20);
ta.setText("Jallon say:\tI'm jallon and glad to see u."+
"\n\t\t"+"If u can't connect me, it means that i'm busy
alking to a lady"
+"\n\t\t"+"wait a moment...Connect again?!\n");
p1.add("North",l1);
p1.add("Center",ta);
add("North",p1);
p2=new Panel();
p2.setLayout(new BorderLayout());
l2=new Label("Guest speak now:");
p2.add("Center",l2);
tf=new TextField(20);
tf.setText("");
p2.add("South",tf);
add("Center",p2);
show();
}
public void start()
{
waitForPackets();
}
public void waitForPackets()
{
try{
sendSocket=new DatagramSocket();
receiveSocket=new DatagramSocket(5001);
}
catch(SocketException se)
{}
while(true)
{
try{
byte array[]=new byte[100];
receivePacket=new DatagramPacket(array,array.len
th);
receiveSocket.receive(receivePacket);
ta.appendText("\nJallon Say:\t");
byte data[]=receivePacket.getData();
String received=new String(data,0);
ta.appendText(received);
}
catch(IOException ioe)
{
ta.appendText("sorry, can't get through...\n");
ta.appendText("problem lies at "+ioe.toString()+
wait and connect again...\n");
}
}
}
public boolean action(Event e,Object o)
{
try{
ta.appendText("\nGuest Say:\t");
String s=o.toString();
ta.appendText(s);
byte data[]=new byte[100];
s.getBytes(0,s.length(),data,0);
sendPacket=new DatagramPacket(data,s.length(),InetAddres
.getByName("202.38.214.236"),5000);
sendSocket.send(sendPacket);
}
catch(IOException ioe)
{
ta.appendText("sorry, net suddently wrong???\n");
ta.appendText("problem lies at "+ioe.toString()+"wait an
connect again...\n");
}
return true;
}
}
--
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -