?? 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 + -