?? chatappletthree.java
字號:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//聊天室主類
public class chatappletthree extends Applet
implements Runnable,ActionListener,ItemListener{
public static final int PORT=1234;//PORT為網絡套接字端口號
static String name,xingbie;//name,xingbie分別為聊天人的名字和性別
Socket socket;
int jilu,enter=0;
//jilu為新進入聊天室的人是否與已有聊天室人重名的狀態標志
//jilu為0表明無重名可以進入;為1表明有重名,應重新進入
//enter為0,未進入聊天室,發送信息按鈕不起作用;enter為1已經進入聊天室
DataInputStream in;//定義讀取服務器信息流 in
static DataOutputStream out;//定義寫入服務器信息流 out
Thread thread;
String line;//line為讀取來自服務器線路的信息
static Mywindow mywindow;
static Apanel a;static Bpanel b;static Cpanel c;
//Applet為驅動初始化畫出聊天室界面,建立與服務器連接
//Applet初始化
public void init(){
mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel();b=new Bpanel();c=new Cpanel();
add("North",a);add("Center",b);add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0;
this.setForeground(Color.black);
c.msg_txt.setBackground(Color.white);
b.chat_txt.setBackground(new Color(200,185,200));
b.chat_txt.setFont(new Font("TimeRoman",Font.PLAIN,12));
}
//Applet小程序驅動
public void start(){
//與服務器建立連接
//默認本機運行服務器端程序,與本機IP建立連接
this.getCodeBase().getHost();
try{
socket=new Socket(this.getCodeBase().getHost(),PORT);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
this.showStatus(e.toString());
say("歡迎來這里!");System.exit(1);
}
say("歡迎來到我的聊天室");
if(thread==null)
{
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
//停止小程序運行
public void stop(){
try{
out.writeUTF("QUIT");
}
catch(IOException e){}
}
//結束Applet關閉網絡套接字連接,結束用戶線程
public void destroy(){
try{
socket.close();}
catch(IOException e){
this.showStatus(e.toString());}
if((thread==null)&&thread.isAlive())
{
thread.yield();
thread=null;
}
}
//-----------------------------------------------------------------------------------------
//定義線程運行操作的方法與服務器通信
public void run()
{ String line; //通過line讀取服務器放入“線路”的信息
try { while(true)
{line=in.readUTF();
//線路信息前端為PEOPLE表明有新人進入了聊天室
if(line.startsWith("PEOPLE"))
{ String listString=line;
if(line.endsWith("*"))
//去掉名字信息后面的*號
{listString=line.substring(0,(line.length()-1));}
b.b2.list.add(listString.substring(6));
if(!line.endsWith("*")) //判斷是否是來自讀取列表的請求
{ b.chat_txt.append(line.substring(6)+"爬上了紅蜘蛛網->"+'\n');}
}
//線路信息前端為QUIT表明有人離開了聊天室
else if (line.startsWith("QUIT"))
{ //QUIT+PEOPLE為10個字符
String str=line.substring(10);
try
{ for(int i=0,k=0;i<=120;i++) //聊天室列表中最多存放120個
{ String s=b.b2.list.getItem(i); //list列表中存放聊天室人員列表
if(s.equals(str)) //判斷某人是離開聊天室的人
{ k=i;b.b2.list.remove(k); //list列表中清除此人
b.chat_txt.append(line.substring(10)+"<-高興地離開了網絡 "+'\n');
}//在聊天文本區顯示某人離開了聊天室
}
}
catch(ArrayIndexOutOfBoundsException e) {}
}
//線路前端為MSG表明接收到的是普通聊天話語信息
else if(line.startsWith("MSG"))
{ b.chat_txt.append(line.substring(3)+'\n');} //在聊天文本區顯示話語
//線路信息前端為“悄悄地對”表明接收到悄悄話
else if(line.startsWith("悄悄地對"))
{
b.chat_txt.append(line+'\n'); //文本區顯示悄悄話
}
}
}
catch(IOException e)
{ say("再見!歡迎再來紅蜘蛛聊天室,如果想重新進入本聊天室,單擊瀏覽器的刷新選項"); }
catch(NullPointerException e) {}
}
//定義聊天室按鈕點擊事件的處理方法
public void actionPerformed(ActionEvent e)
{
//點擊了進入聊天室按鈕Apanel中的a.button1
if(e.getSource()==a.button1)
{
name=new String(a.name_txt.getText());
try
{ for(int i=0;i<=120;i++) //判斷是否與現有的聊天室人員重名
{ if((a.name_txt.getText()!=null)&&((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||a.name_txt.getText().equals("該名字已被使用")))
{ jilu=1;name=null;break; }
}
}
catch(ArrayIndexOutOfBoundsException e3) {}
if(jilu==0)
{ try
{ out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
enter=1;
}
//向服務器寫入進入聊天室信息,enter標志置1,已進入聊天室
catch(IOException e1){}
}
else if(jilu==1)
{a.name_txt.setText("該名字已被使用");}
jilu=0;
}
//點擊了離開聊天室按鈕Apanel中的a.button2
else if(e.getSource()==a.button2)
{ try
{// 離開聊天室,enter標志置0
out.writeUTF("QUIT");enter=0;
}
catch(IOException e1) {}
b.b2.list.removeAll();//清除聊天人員列表
}
//如果是點擊了聊天信息發送按鈕Cpanle中的c.button
else if (e.getSource()==c.button&&enter==1)
{ if(name!=null)
{ try
{ out.writeUTF("MSF"+name+"["+xingbie+"]"+"說->"+":"+c.msg_txt.getText());
c.msg_txt.setText(null);//發送完畢信息清空寫信息文本框
}
catch(IOException e1) {}
}
}
//雙擊了聊天人員列表中某人B2panel中的list彈出窗口發送悄悄話
else if(e.getSource()==b.b2.list&&enter==1)
{
mywindow.setVisible(true);
mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
}
//雙擊了刷新談話區按鈕Cpanel中的c.button2
else if(e.getSource()==c.button2)
{ b.chat_txt.setText(null); }//清空聊天文本區
//點擊了刷新聊天列表區按鈕Cpanel中的c.button3
else if(e.getSource()==c.button3)
{ try
{b.b2.list.removeAll();out.writeUTF("newlist");}//刷新聊天人員列表
catch(IOException e1) {}
}
}
//性別選擇框事件的處理
public void itemStateChanged(ItemEvent e1)
{
if(e1.getItemSelectable()==a.box1)
{xingbie=new String("男");}
else if(e1.getItemSelectable()==a.box2)
{xingbie=new String("女");}
else if(e1.getItemSelectable()==a.box3)
{xingbie=new String("蛋蛋");}
}
//各種事件系統消息顯示在文本區say方法
public void say(String msg)
{
b.chat_txt.append("****"+msg+"****\n");
}
}
//-----------------------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -