?? chatapplet.java
字號(hào):
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class chatApplet extends Applet {
/*以下用于定義UI變量*/
Panel panel1 = new Panel(); //用于放置輸入姓名和連接兩個(gè)按鈕
BorderLayout borderLayout1 = new BorderLayout();
Panel panel2 = new Panel(); //用于放置聊天信息顯示和聊天人員列表
Panel panel3 = new Panel(); //用于放置發(fā)送信息區(qū)域
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
Label label1 = new Label();
TextField name_txt = new TextField(15);
Button button1 = new Button();
Button button2 = new Button();
TextArea chat_txt = new TextArea(15,30);
Label label2 = new Label();
Button button3 = new Button();
TextField msg_txt = new TextField(20);
java.awt.List list1 = new java.awt.List(13);
/*以下定義數(shù)據(jù)流和網(wǎng)絡(luò)變量*/
Socket soc=null; //定義連接套接字
PrintStream ps=null; //定義打印流
Listen listen=null; //定義一個(gè)客戶(hù)端線程
public void init() //初始化圖形界面
{
resize(475,350);
this.setLayout(borderLayout1);
panel2.setLayout(flowLayout1);
panel3.setLayout(flowLayout2);
label1.setText("姓名:");
button1.setLabel("連接");
button2.setLabel("斷開(kāi)連接");
chat_txt.setEditable(false);
panel2.setBackground(Color.cyan);
panel1.setBackground(Color.cyan);
label2.setText("聊天信息:");
button3.setLabel("發(fā)送");
msg_txt.setText("請(qǐng)輸入聊天信息");
panel3.setBackground(Color.cyan);
this.add(panel1, BorderLayout.NORTH);
panel1.add(label1, null);
panel1.add(name_txt, null);
panel1.add(button1, null);
panel1.add(button2, null);
this.add(panel2, BorderLayout.CENTER);
panel2.add(chat_txt, null);
panel2.add(list1, null);
this.add(panel3, BorderLayout.SOUTH);
panel3.add(label2, null);
panel3.add(msg_txt, null);
panel3.add(button3, null);
}
public boolean action(Event evt,Object obj) //事件觸發(fā)代碼
{
if(evt.target instanceof Button)
{
String label=(String) obj;
if(label.equals("連接")) //如果點(diǎn)擊連接后
{
if(soc==null)
{
try
{
soc=new Socket(InetAddress.getLocalHost(),2525); //使用端口2525實(shí)例化一個(gè)本地套接字
System.out.println(soc); //在控制臺(tái)打印實(shí)例化的結(jié)果
ps=new PrintStream(soc.getOutputStream()); //將ps指向soc的輸出流
StringBuffer info=new StringBuffer("INFO: "); //定義一個(gè)字符緩沖存儲(chǔ)發(fā)送信息
//其中INFO為關(guān)鍵字讓服務(wù)器識(shí)別為連接信息
//并將name和ip用":"分開(kāi),在服務(wù)器端將用一個(gè)
//StringTokenizer類(lèi)來(lái)讀取數(shù)據(jù)
String userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString();
ps.println(info.append(userinfo));
ps.flush();
listen=new Listen(this,name_txt.getText(),soc); //將客戶(hù)端線程實(shí)例化
listen.start(); //啟動(dòng)線程
}
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
} //end of if
}//end of if
else if(label.equals("斷開(kāi)連接")) //如果點(diǎn)擊斷開(kāi)連接按鈕則運(yùn)行disconnect()
{
disconnect();
}
else if(label.equals("發(fā)送")) //如果點(diǎn)擊發(fā)送按鈕
{
if(soc!=null)
{
StringBuffer msg=new StringBuffer("MSG: "); //定義并實(shí)例化一個(gè)字符緩沖存儲(chǔ)發(fā)送的聊天信息
//其中MSG為關(guān)鍵詞
try
{
String msgtxt=new String(msg_txt.getText());
}
catch(Exception e){}
ps.println(msg.append(msg_txt.getText())); //用打印流發(fā)送聊天信息
ps.flush();
}
}
}
return true;
} //end of method action
public void disconnect() //客戶(hù)端點(diǎn)擊斷開(kāi)連接要運(yùn)行的方法
{
if(soc!=null)
{
try
{
listen.suspend();
ps.println("QUIT"); //用打印流發(fā)送QUIT信息通知服務(wù)器斷開(kāi)此次通信
ps.flush();
soc.close(); //關(guān)閉套接字
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
finally
{
}
}// end of if
}
class Listen extends Thread //客戶(hù)端線程類(lèi)用來(lái)監(jiān)聽(tīng)服務(wù)器傳來(lái)的信息
{
String name=null; //用來(lái)存儲(chǔ)客戶(hù)端連接后的name信息
DataInputStream dis=null; //用來(lái)實(shí)現(xiàn)客戶(hù)端接受服務(wù)器數(shù)據(jù)的輸入流
PrintStream ps=null; //用來(lái)實(shí)現(xiàn)從客戶(hù)端發(fā)送數(shù)據(jù)到服務(wù)器的打印流
Socket socket=null; //用來(lái)存儲(chǔ)客戶(hù)端的socket信息
chatApplet parent=null; //用來(lái)存儲(chǔ)當(dāng)前運(yùn)行的chatApplet實(shí)例
public Listen(chatApplet p,String n,Socket s) //Listen類(lèi)的構(gòu)造器
{
//接受參數(shù)
parent=p;
name=n;
socket=s;
try
{
//實(shí)例化兩個(gè)數(shù)據(jù)流
dis=new DataInputStream(s.getInputStream());
ps=new PrintStream(s.getOutputStream());
}
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
} //end of Listen constractor
public void run() //線程運(yùn)行方法
{
String msg=null;
while(true)
{
try{msg=dis.readLine();} //讀取從服務(wù)器傳來(lái)的信息
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
if (msg==null) //如果從服務(wù)器傳來(lái)的信息為空則斷開(kāi)此次連接
{
parent.listen=null;
parent.soc=null;
parent.list1.clear();
return;
}
StringTokenizer st=new StringTokenizer(msg,":"); //用StringTokenizer類(lèi)來(lái)實(shí)現(xiàn)讀取分段字符
String keyword=st.nextToken(); //讀取信息頭即關(guān)鍵字用來(lái)識(shí)別是何種信息
if(keyword.equals("PEOPLE")) //如果是PEOPLE則是服務(wù)器發(fā)來(lái)的客戶(hù)連接信息
//主要用來(lái)刷新客戶(hù)端的用戶(hù)列表
{
parent.list1.clear();
while(st.hasMoreTokens()) //遍歷st取得目前所連接的客戶(hù)
{
String str=st.nextToken();
parent.list1.addItem(str);
}
}
else if(keyword.equals("MSG")) //如果關(guān)鍵字是MSG則是服務(wù)器傳來(lái)的聊天信息
//主要用來(lái)刷新客戶(hù)端聊天信息區(qū)將每個(gè)客戶(hù)的聊天內(nèi)容顯示出來(lái)
{
String usr=st.nextToken();
parent.chat_txt.appendText(usr);
parent.chat_txt.appendText(st.nextToken("\0"));
parent.chat_txt.appendText("\n\n");
}
else if(keyword.equals("QUIT")) //如果關(guān)鍵字是QUIT則是服務(wù)器關(guān)閉的信息
//用來(lái)切斷此次連接
{
System.out.println("Quit");
try
{parent.listen.stop();
parent.listen=null;
parent.soc.close();
parent.soc=null;
}catch(IOException e)
{
System.out.println("Error:"+e);
}
parent.list1.clear();
return;
}
}
} //end of run method
} //end of Listen inner class
} //end of chatApplet class
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -