📄 chatroom.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
/*<p>Title:HappyChat聊天系统聊天程序</p>
*<p>Description:系统用户登录后进行聊天</p>
*<p>Copyright:Copyright (C)2006</p>
*<p>Filename:ChatRoom.java</p>
*@author 刘志成
*@version 1.0
*/
public class ChatRoom extends Thread implements ActionListener
{
static JFrame frmChat;
JPanel pnlChat;
JButton btnCls,btnExit,btnSend,btnClear,btnSave,btnTimer,btnSendFile,btnBrowse;
JLabel lblUserList,lblUserMessage,lblSendMessage,lblChatUser;
JLabel lblUserTotal,lblCount,lblBack,lblFile;
JTextField txtMessage,txtFile;
java.awt.List lstUserList;
TextArea taUserMessage;
JComboBox cmbUser;
JCheckBox chPrivateChat;
String strServerIp,strLoginName;
Thread thread;
JMenuBar mbChat;
JMenu mnuSystem,mnuHelp;
JMenuItem mnuiCls,mnuiSave,mnuiClock,mnuiExit,mnuiContent,mnuiIndex,mnuiAbout;
//用于将窗口用于定位
Dimension scrnsize;
Toolkit toolkit=Toolkit.getDefaultToolkit();
//构造方法
public ChatRoom(String name,String ip)
{
strServerIp=ip;
strLoginName=name;
frmChat=new JFrame("聊天室"+"[用户:"+name+"]");
pnlChat=new JPanel();
frmChat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmChat.getContentPane().add(pnlChat);
Font fntDisp1=new Font("宋体",Font.PLAIN,12);
//Font fntDisp2=new Font("宋体",Font.PLAIN,11);
mbChat=new JMenuBar();
mnuSystem=new JMenu("系统(S)");
mnuSystem.setMnemonic(KeyEvent.VK_S);
mnuSystem.setFont(fntDisp1);
mnuHelp=new JMenu("帮助(H)");
mnuHelp.setMnemonic(KeyEvent.VK_H);
mnuHelp.setFont(fntDisp1);
mbChat.add(mnuSystem);
mbChat.add(mnuHelp);
mnuiCls=new JMenuItem("清除屏幕显示");
mnuiCls.setFont(fntDisp1);
mnuiSave=new JMenuItem("保存聊天记录");
mnuiSave.setFont(fntDisp1);
mnuiClock=new JMenuItem("查看时间");
mnuiClock.setFont(fntDisp1);
mnuiExit=new JMenuItem("退出系统");
mnuiExit.setFont(fntDisp1);
mnuSystem.add(mnuiCls);
mnuSystem.add(mnuiSave);
mnuSystem.add(mnuiClock);
mnuSystem.add(mnuiExit);
mnuiContent=new JMenuItem("目录");
mnuiContent.setFont(fntDisp1);
mnuiIndex=new JMenuItem("索引");
mnuiIndex.setFont(fntDisp1);
mnuiAbout=new JMenuItem("关于[HappyChat聊天系统]...");
mnuiAbout.setFont(fntDisp1);
mnuHelp.add(mnuiContent);
mnuHelp.add(mnuiIndex);
mnuHelp.add(mnuiAbout);
frmChat.setJMenuBar(mbChat);
String list[]={"所有人"};
btnCls=new JButton("清屏(C)");
btnExit=new JButton("退出(X)");
btnSend=new JButton("发送(N)");
btnSave=new JButton("保存(S)");
btnTimer=new JButton("时钟(T)");
btnSendFile=new JButton("发送文件(F)");
btnBrowse=new JButton("浏览...");
lblUserList=new JLabel("【在线用户列表】");
lblUserMessage=new JLabel("【聊天信息】");
lblSendMessage=new JLabel("聊天内容:");
lblChatUser=new JLabel("你对:");
lblUserTotal=new JLabel("在线人数:");
lblCount=new JLabel("0");
lblFile=new JLabel("附件:");
txtFile=new JTextField(20);
lstUserList=new java.awt.List();
txtMessage=new JTextField(170);
cmbUser =new JComboBox(list);
chPrivateChat =new JCheckBox("私聊");
taUserMessage=new TextArea("",300,200,TextArea.SCROLLBARS_VERTICAL_ONLY);//只能向下滚动
taUserMessage.setEditable(false); //不可写入
/* 该布局采用手动布局 *
* setBounds设置组件位置 *
* setFont设置字体、字型、字号 *
* setForeground设置文字的颜色 *
* setBackground设置背景色 *
* setOpaque将背景设置为透明 */
pnlChat.setLayout(null);
pnlChat.setBackground(new Color(52,130,203));
btnSendFile.setBounds(250,360,100,25);
btnBrowse.setBounds(170,360,80,25);
btnTimer.setBounds(400,360,80,25);
btnSave.setBounds(500,330,80,25);
btnCls.setBounds(400,330,80,25);
btnExit.setBounds(500,360,80,25);
btnSend.setBounds(500,300,80,25);
lblFile.setBounds(10,330,40,25);
lblUserList.setBounds(5,0,120,40);
lblUserTotal.setBounds(130,0,60,40);
lblCount.setBounds(190,0,60,40);
lblUserMessage.setBounds(225,0,180,40);
lblChatUser.setBounds(10,290,40,40);
lblSendMessage.setBounds(210,290,60,40);
//lblUserTotal.setBounds(10,340,100,40);
//lblCount.setBounds(73,340,100,40);
txtFile.setBounds(50,330,300,25);
lstUserList.setBounds(5,40,210,255);
taUserMessage.setBounds(225,40,360,255);
txtMessage.setBounds(270,300,210,25);
cmbUser.setBounds(50,300,80,25);
chPrivateChat.setBounds(135,302,60,20);
btnSendFile.setFont(fntDisp1);
btnBrowse.setFont(fntDisp1);
btnTimer.setFont(fntDisp1);
btnCls.setFont(fntDisp1);
btnExit.setFont(fntDisp1);
btnSend.setFont(fntDisp1);
btnSave.setFont(fntDisp1);
lblFile.setFont(fntDisp1);
lblUserList.setFont(fntDisp1);
lblUserMessage.setFont(fntDisp1);
lblChatUser.setFont(fntDisp1);
lblSendMessage.setFont(fntDisp1);
lblUserTotal.setFont(fntDisp1);
lblCount.setFont(fntDisp1);
txtFile.setFont(fntDisp1);
cmbUser.setFont(fntDisp1);
chPrivateChat.setFont(fntDisp1);
taUserMessage.setFont(new Font("宋体",Font.PLAIN,12));
lblUserList.setForeground(Color.YELLOW);
lblUserMessage.setForeground(Color.YELLOW);
lblSendMessage.setForeground(Color.black);
lblChatUser.setForeground(Color.black);
lblSendMessage.setForeground(Color.black);
lblUserTotal.setForeground(Color.YELLOW);
lblCount.setForeground(Color.YELLOW);
cmbUser.setForeground(Color.black);
chPrivateChat.setForeground(Color.black);
lstUserList.setBackground(Color.white);
taUserMessage.setBackground(Color.white);
btnBrowse.setBackground(Color.PINK);
btnSendFile.setBackground(Color.PINK);
btnTimer.setBackground(Color.ORANGE);
btnCls.setBackground(Color.ORANGE);
btnExit.setBackground(Color.ORANGE);
btnSend.setBackground(Color.PINK);
btnSave.setBackground(Color.ORANGE);
pnlChat.add(lblFile);
pnlChat.add(txtFile);
pnlChat.add(btnSendFile);
pnlChat.add(btnBrowse);
pnlChat.add(btnTimer);
pnlChat.add(btnCls);
pnlChat.add(btnExit);
pnlChat.add(btnSend);
pnlChat.add(btnSave);
pnlChat.add(lblUserList);
pnlChat.add(lblUserMessage);
pnlChat.add(lblSendMessage);
pnlChat.add(lblChatUser);
pnlChat.add(lblUserTotal);
pnlChat.add(lblCount);
pnlChat.add(lstUserList);
pnlChat.add(taUserMessage);
pnlChat.add(txtMessage);
pnlChat.add(cmbUser);
pnlChat.add(chPrivateChat);
frmChat.addWindowListener(new Windowclose());
btnTimer.addActionListener(this);
btnCls.addActionListener(this);
btnExit.addActionListener(this);
btnSend.addActionListener(this);
btnSave.addActionListener(this);
lstUserList.addActionListener(this);
txtMessage.addActionListener(this);
//启动聊天页面信息刷新线程
Thread thread = new Thread(this);
thread.start();
// Icon log=new ImageIcon("images\\chat.jpg");
// lblBack = new JLabel(log);
// lblBack.setBounds(1, 1, 600,420);
// pnlChat.add(lblBack);
frmChat.setSize(600,440);
frmChat.setVisible(true);
frmChat.setResizable(false);
//将窗口定位在屏幕中央
scrnsize=toolkit.getScreenSize();
frmChat.setLocation(scrnsize.width/2-frmChat.getWidth()/2,
scrnsize.height/2-frmChat.getHeight()/2);
Image img=toolkit.getImage("images\\appico.jpg");
frmChat.setIconImage(img);
} //构造方法结束
public void run()
{
int intMessageCounter=0;
int intUserTotal=0;
boolean isFirstLogin=true; //判断是否刚登陆
boolean isFound; //判断是否找到用户
Vector user_exit=new Vector();
try
{
for(;;)
{
Socket toServer;
toServer=new Socket(strServerIp,1001);
//将信息发往服务器
Message messobj=new Message();
ObjectOutputStream streamtoserver=new ObjectOutputStream(toServer.getOutputStream());
streamtoserver.writeObject((Message)messobj);
//收来自服务器的信息
ObjectInputStream streamfromserver=new ObjectInputStream(toServer.getInputStream());
messobj=(Message)streamfromserver.readObject();
////////刷新聊天信息列表//////////
if (isFirstLogin) //如果刚登陆
{
intMessageCounter=messobj.chat.size(); //屏蔽该用户登陆前的聊天内容
isFirstLogin=false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -