📄 chatframe.java
字号:
package chat;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.Hashtable;
import javax.swing.*;
public class ChatFrame extends JFrame implements ActionListener,Runnable{
private Socket socket_c=null;
private DataInputStream in=null;
private DataOutputStream out=null;
private static Hashtable listTable; //存放在线聊天者ID和名称的散列表
public int Width=1000;
public int Height=600;
public String name;//聊天者姓名
public String ID;//聊天者ID
Thread threadMessage=null;//读取服务器信息的线程
JTextArea publicshowmessage=null;//公共谈话显示区
JTextArea privateshowmessage=null;//私聊谈话显示区
JTextField sendmessage=null;//发送的消息
JButton sure;
JButton exit;
JButton refurbishpublic;
JButton refurbishprivate;
List listComponent_on=null;//显示在线聊天者名称的组件
List listComponent_all=null;//显示全部注册聊天者名称组件
Choice privateChatList;//选择私人聊天者的下拉列表
public ChatFrame(Socket socket_l,String a,String b,Hashtable c)throws IOException{
super("飞腾欢迎您");
socket_c=socket_l;
ID=a;
name=b;
listTable=c;
threadMessage=new Thread(this);
setSize(Width,Height);
setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel north=new JPanel();
north.setLayout(new GridLayout(2,1));
JLabel ID=new JLabel(a);
JLabel name=new JLabel(b);
JLabel sign=new JLabel("双击聊天者可私聊");
JLabel publicarea=new JLabel("公共聊天区域");
JLabel privatearea=new JLabel("私人聊天区域");
JLabel namelist=new JLabel("在线好友");
JLabel nameAll=new JLabel("全部好友");
JPanel north1=new JPanel();
north1.setLayout(new GridLayout(1,3));
north1.add(ID);
north1.add(name);
north1.add(sign);
JPanel north2=new JPanel();
north2.setLayout(new GridLayout(1,4));
north2.add(publicarea);
north2.add(privatearea);
north2.add(namelist);
north2.add(nameAll);
north.add(north1);
north.add(north2);
JPanel center=new JPanel();
center.setLayout(new GridLayout(1,4));
center.setBackground(Color.orange);
publicshowmessage=new JTextArea(10,10);
publicshowmessage.setBackground(Color.WHITE);
publicshowmessage.setForeground(Color.RED);
publicshowmessage.setEditable(false);
publicshowmessage.setLineWrap(true);
JScrollPane publicscrolledText=new JScrollPane(publicshowmessage);
publicscrolledText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
center.add(publicscrolledText);
privateshowmessage=new JTextArea(10,10);
privateshowmessage.setBackground(Color.WHITE);
privateshowmessage.setForeground(Color.ORANGE);
privateshowmessage.setEditable(false);
privateshowmessage.setLineWrap(true);
JScrollPane privatescrolledText=new JScrollPane(privateshowmessage);
privatescrolledText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
center.add(privatescrolledText);
listComponent_on=new List();
listComponent_on.addActionListener(this);//双击列表中的聊天者名称,可选中与之私聊
JScrollPane listComponent_onscro=new JScrollPane(listComponent_on);
listComponent_onscro.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
center.add(listComponent_onscro);
listComponent_all=new List();
listComponent_all.addActionListener(this);//双击列表中的聊天者名称,可选中与之私聊
JScrollPane listComponent_allscro=new JScrollPane(listComponent_all);
listComponent_allscro.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
center.add(listComponent_allscro);
JPanel south=new JPanel();
south.setLayout(new GridLayout(1,2));
sendmessage=new JTextField(50);
sendmessage.addActionListener(this);
south.add(sendmessage);
JPanel southeast=new JPanel();
southeast.setLayout(new GridLayout(1,5));
sure=new JButton("送出信息到");
sure.addActionListener(this);
southeast.add(sure);
privateChatList=new Choice();
privateChatList.add("大家(*)");
privateChatList.select(0); //默认情况下,用户输入内容发送给所有聊天者
southeast.add(privateChatList);
refurbishpublic=new JButton("刷新公话区");
refurbishpublic.addActionListener(this);
southeast.add(refurbishpublic);
refurbishprivate=new JButton("刷新私聊区");
refurbishprivate.addActionListener(this);
southeast.add(refurbishprivate);
exit=new JButton("退出");
exit.addActionListener(this);
southeast.add(exit);
south.add(southeast);
contentPane.add(north,BorderLayout.NORTH);
contentPane.add(center,BorderLayout.CENTER);
contentPane.add(south,BorderLayout.SOUTH);
in=new DataInputStream(socket_c.getInputStream());
out=new DataOutputStream(socket_c.getOutputStream());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
try{//启动线程
threadMessage.start();
}
catch(Exception e){
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==sure||e.getSource()==sendmessage){
String message;
String people=privateChatList.getSelectedItem();
String aimname=people;
people=people.substring(people.indexOf("(")+1,people.indexOf(")"));//获取信息发送对象的ID
message=sendmessage.getText();
if(message.length()>0){//将聊天内容及对象发送给服务器
try{
if(people.equals("*")){
out.writeUTF("公共聊天内容:"+name+"("+ID+")对大家说 "+message);
}
else {
out.writeUTF("私人聊天内容:"+name+"("+ID+")悄悄地对"+aimname+"说 "+message+"#"+people);
privateshowmessage.append(name+"("+ID+")悄悄地对"+aimname+"说 "+message+"\n");
}
}
catch(IOException event){
}
}
sendmessage.setText(null);
}
else if(e.getSource()==exit){
try{
out.writeUTF("用户离开");
System.exit(0);
}
catch(IOException event){
}
}
else if(e.getSource()==listComponent_on){
String k=listComponent_on.getSelectedItem();
for(int i=0;i<privateChatList.getItemCount();i++){
if(k.equals(privateChatList.getItem(i))){
privateChatList.remove(i);
break;
}
}
privateChatList.insert(listComponent_on.getSelectedItem(),0);
privateChatList.repaint();
}
else if(e.getSource()==listComponent_all){
String k=listComponent_all.getSelectedItem();
for(int j=0;j<privateChatList.getItemCount();j++){
if(k.equals(privateChatList.getItem(j))){
privateChatList.remove(j);
break;
}
}
privateChatList.insert(listComponent_all.getSelectedItem(),0);
privateChatList.repaint();
}
else if(e.getSource()==refurbishpublic){
publicshowmessage.setText(null);
}
else if(e.getSource()==refurbishprivate){
privateshowmessage.setText(null);
}
}
public void run(){
while(true){
String s=null;
try{
s=in.readUTF(); //等待(阻塞本线程,直到收到信息)服务器信息
if(s.startsWith("公共聊天内容:")){//读取服务器发送来的信息
String content=s.substring(s.indexOf(":")+1);
publicshowmessage.append(content+"\n");
}
else if(s.startsWith("私人聊天内容:")){//读取服务器发来的信息
String content=s.substring(s.indexOf(":")+1);
privateshowmessage.append(content+"\n");
}
else if(s.startsWith("私人留言信息")){//读取别人给本用户的私人信息
String content=s.substring(s.indexOf(":")+1);
privateshowmessage.append(content+"\n");
}
else if(s.startsWith("公共留言信息")){//读取公共留言信息
String content=s.substring(s.indexOf(":")+1);
publicshowmessage.append(content+"\n");
}
else if(s.startsWith("聊天者")){//显示新加入的聊天者信息
String people=s.substring(s.indexOf(":")+1,s.indexOf("("));
String ID=s.substring(s.indexOf("(")+1,s.indexOf(")"));
listComponent_on.setForeground(Color.BLUE);
//先将名称和ID存放到散列表(关键是ID)
listTable.put(ID,people+"("+ID+")");
//再将名称添加到List组件中
listComponent_on.add((String)listTable.get(ID));
listComponent_on.repaint();
}
else if(s.startsWith("注册者")){//显示在线注册的用户
String registerinf=s.substring(s.indexOf(":")+1);
listComponent_all.add(registerinf);
listComponent_all.repaint();
}
else if(s.startsWith("上线")){
String content=s.substring(s.indexOf(":")+1);
publicshowmessage.append(content+"\n");
//privateshowmessage.append(content+"\n");
}
else if(s.startsWith("用户离线:")){//删除已离线的聊天者信息
String awayPeopleID=s.substring(s.indexOf("(")+1,s.indexOf(")"));
listComponent_on.remove((String)listTable.get(awayPeopleID));
listComponent_on.repaint();
publicshowmessage.append((String)listTable.get(awayPeopleID)+"离线"+"\n");
listTable.remove(awayPeopleID);
}
Thread.sleep(5);
}
catch(IOException e){ //服务器关闭套接字连接时,导致IOException异常
listComponent_on.removeAll();
listComponent_on.repaint();
listComponent_all.removeAll();
listComponent_all.repaint();
listTable.clear();
publicshowmessage.setText("服务器出现异常.");
break;
}
catch(InterruptedException e){
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -