📄 chatapplet.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class chatApplet extends Applet {
Panel panel1 = new Panel(); //用于放置输入姓名和连接两个按钮
BorderLayout borderLayout1 = new BorderLayout();
Panel panel2 = new Panel();
Panel panel3 = new Panel(); //用于放置发送信息区域
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
Label label1 = new Label();
// Label label11 = 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);
Socket soc=null; //定义连接套接字
PrintStream ps=null;
Listen listen=null;
public void init() //初始化图形界面
{
resize(475,350);
this.setLayout(borderLayout1);
panel2.setLayout(flowLayout1);
panel3.setLayout(flowLayout2);
label1.setText("姓名:");
//label11.setText("socket简易聊天室");
button1.setLabel("连接");
button2.setLabel("断开连接");
chat_txt.setEditable(false);
panel2.setBackground(Color.cyan);
panel1.setBackground(Color.cyan);
label2.setText("聊天信息:");
button3.setLabel("发送");
msg_txt.setText("please input a message");
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) //事件触发代码
{
if(evt.target instanceof Button)
{
String label=(String) obj;
if(label.equals("连接"))
{
if(soc==null)
{
try
{
soc=new Socket(InetAddress.getLocalHost(),2525); //使用端口2525实例化一个本地套接字
System.out.println(soc);
ps=new PrintStream(soc.getOutputStream());
StringBuffer info=new StringBuffer("INFO: ");
//其中INFO为关键字让服务器识别为连接信息
String userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString();
ps.println(info.append(userinfo));
ps.flush();
listen=new Listen(this,name_txt.getText(),soc);
listen.start(); //启动线程
}
catch(IOException e)
{
System.out.println("错误:"+e);
disconnect();
}
} //end of if
}//end of if
else if(label.equals("断开连接")) //如果点击断开连接按钮则运行disconnect()
{
disconnect();
}
else if(label.equals("发送")) //如果点击发送按钮
{
if(soc!=null)
{
StringBuffer msg=new StringBuffer("MSG: "); //定义并实例化一个字符缓冲存储发送的聊天信息
try
{
String msgtxt=new String(msg_txt.getText());
}
catch(Exception e){}
ps.println(msg.append(msg_txt.getText()));
ps.flush();
}
}
}
return true;
} //end of method action
public void disconnect()
{
if(soc!=null)
{
try
{
listen.suspend();
ps.println("QUIT");
ps.flush();
soc.close(); //关闭套接字
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
finally
{
}
}// end of if
}
class Listen extends Thread //客户端线程类用来监听服务器传来的信息
{
String name=null;
DataInputStream dis=null;
PrintStream ps=null;
Socket socket=null;
chatApplet parent=null; //用来存储当前运行的chatApplet实例
public Listen(chatApplet p,String n,Socket s)
{
parent=p;
name=n;
socket=s;
try
{
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() //线程运行方法
{
String msg=null;
while(true)
{
try{msg=dis.readLine();}
// msg=dis.readUTF();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
parent.disconnect();
}
if (msg==null) //如果从服务器传来的信息为空则断开此次连接
{
parent.listen=null;
parent.soc=null;
parent.list1.clear();
return;
}
StringTokenizer st=new StringTokenizer(msg,":");
String keyword=st.nextToken();
if(keyword.equals("PEOPLE"))
{
parent.list1.clear();
while(st.hasMoreTokens()) //遍历st取得目前所连接的客户
{
String str=st.nextToken();
parent.list1.addItem(str);
}
}
else if(keyword.equals("MSG"))
{
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"))
//用来切断此次连接
{
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -