?? chatarea.java~970~
字號:
jpanel1.setBackground(bgcolor);
jpanel1.setBorder(new OvalBorder(15, 5, Color.black, Color.black));
jpanel1.add(jpanel2, BorderLayout.WEST);
jpanel1.add(jpanel3, BorderLayout.EAST);
jpanel2.add(messageEditor);
jpanel2.setBackground(bgcolor);
jpanel3.setBackground(bgcolor);
jpanel3.setLayout(new BorderLayout());
jpanel3.add(sendMessageButton, BorderLayout.WEST);
jpanel3.add(sendObject, BorderLayout.CENTER);
this.setLayout(borderLayout1);
this.add(jmenuBar, BorderLayout.NORTH);
this.add(jsplitPane2, BorderLayout.CENTER);
//添加菜單事建處理
loginOut.addActionListener(action);
sendFile.addActionListener(action);
gongzuo.addActionListener(revertAction);
chifan.addActionListener(revertAction);
youshi.addActionListener(revertAction);
zidingyi.addActionListener(revertAction);
quxiao.addActionListener(revertAction);
consoleMenuItem.addActionListener(consoleAction);
chatLogPriMenuItem.addActionListener(chatLogAction);
chatLogPubMenuItem.addActionListener(chatLogAction);
chatLogCloseMenuItem.addActionListener(chatLogAction);
sendMessageButton.addActionListener(action);
clearSendObjectButton.addActionListener(action);
//用戶在線列表鼠標事件
peopleList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
JList list = (JList) me.getSource();
if (me.getClickCount() == 1) {//單擊選擇對象
selectName = (String) peopleList.getSelectedValue();
if (!toPeople.contains(selectName)) {
toPeople.addElement(selectName);
selectNameSB.append(selectName + " ");
ChatArea.sendObject.setText(selectNameSB.toString());
}
}
else if (me.getClickCount() == 2) {//雙擊取消選定對象
selectName = (String) peopleList.getSelectedValue();
if (toPeople.contains(selectName) && selectName != null) {
toPeople.removeElement(selectName);
selectNameSB.delete(selectNameSB.indexOf(selectName),
selectNameSB.indexOf(selectName) +
selectName.length() + 1);
ChatArea.sendObject.setText(selectNameSB.toString());
}
}
}
});
messageThread = new Thread(this);
//設置鼠標靠近提示信息
peopleList.setToolTipText(constant.getLang("CA_peopleList"));
sendObject.setToolTipText(constant.getLang("CA_sendObject"));
}
/**
* 連接服務器方法
* @param socket Socket 客戶套接
* @param out DataOutputStream 輸出流
* @param in DataInputStream 輸入流
*/
public void setConnection(Socket socket, DataOutputStream out,
DataInputStream in) {
this.socket = socket;
this.out = out;
this.in = in;
loginMenu.setEnabled(true);
fileMenu.setEnabled(true);
revertMenu.setEnabled(true);
consoleMenu.setEnabled(true);
chatLogMenu.setEnabled(true);
sendMessageButton.setEnabled(true);
clearSendObjectButton.setEnabled(true);
userName = loginDialog.getName();
try {
messageThread.start();//啟動當前線程
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 取得 socket對象
* @return Socket socket對象
*/
public static Socket getSocket() {
return socket;
}
/**
* 取得輸出流
* @return DataOutputStream 輸出流
*/
public static DataOutputStream getOut() {
return out;
}
/**
* 線程執行體
*/
public void run() {
while (true) {//無限循環,一直偵聽
String _message = null;//接收的消息
try {
_message = in.readUTF();//取得接收到的消息
if (_message.startsWith("PRIVATE_MESSAGE:")) {//私人消息
String _headMessage = _message.substring(_message.indexOf(
"PRIVATE_MESSAGE:") + 16, _message.indexOf("#CONTENT#"));//取得頭消息
String _content = _message.substring(_message.indexOf("#CONTENT#") +
9);//聊天內容
try {
StringReader stringReader = new StringReader(
"<p><font color=\"#0080C0\">^" + _headMessage + "^</font></p>" +
_content);//把顯示內容轉化成流對象
editorKit.read(stringReader, privateDoc, privateDoc.getLength());//把聊天內容顯示在私人區
}
catch (BadLocationException e) {
}
if (REVERTOPTION == 1) {//如果用戶設置的是自動回復方式
String _fromPeople = _headMessage.substring(0,
_headMessage.indexOf("對您說:"));
out.writeUTF("PRIVATE_MESSAGE:" + "#TOPEOPLE#" + _fromPeople +
"#MESSAGECONTENT#" + REVERTMESSAGE);//輸出自動回復信息
}
if (tablePane.getSelectedIndex() != 1) {//如果用戶當前查看的信息面板不是私人區,設置提示圖片
tablePane.setIconAt(1, privateTableNew);
}
}
else if (_message.startsWith("PUBLIC_MESSAGE:")) {//公共消息
String _headMessage = _message.substring(_message.indexOf(
"PUBLIC_MESSAGE") + 15, _message.indexOf("#CONTENT#"));//取得頭消息
String content = _message.substring(_message.indexOf("#CONTENT#") + 9);//取得聊天內容
try {
StringReader stringReader = new StringReader(
"<p><font color=\"#0080C0\">^" + _headMessage + "^</font></p>" +
content);//轉換成流對象
editorKit.read(stringReader, publicDoc, publicDoc.getLength());//顯示信息
}
catch (BadLocationException e) {
}
if (REVERTOPTION == 1) {//當前用戶設置了自動回復方式
String _fromPeople = _headMessage.substring(0,
_headMessage.indexOf("對大家說:"));//取得發送消息的對象
out.writeUTF("PRIVATE_MESSAGE:" + "#TOPEOPLE#" + _fromPeople +
"#MESSAGECONTENT#" + REVERTMESSAGE);//發送消息給相應的對象
}
if (tablePane.getSelectedIndex() != 0) {//當前查看的面板不是公共的,設置提示信息
tablePane.setIconAt(0, publicTableNew);
}
}
else if (_message.startsWith("LOGIN_PEOPLE:")) {//用戶登陸
String peopleName = _message.substring(_message.indexOf(":") + 1);//取得用戶名
peopleTable.put(peopleName, peopleName);//添加進hashtable
model.addElement( (String) peopleTable.get(peopleName));//把對象添加到list 的model對象中
peopleList.validate();//刷新顯示
}
else if (_message.startsWith("SEND_FILE:")) {//發送文件
String _requestPeople = _message.substring(_message.indexOf(
"SEND_FILE:") + 10, _message.indexOf("#TOSENDFILE#"));//請求發送文件的對象
String _toSendFile = _message.substring(_message.indexOf(
"#TOSENDFILE#") + 12, _message.indexOf("#IPADDRESS#"));//請求發送的文件名
String _ipAddress = _message.substring(_message.indexOf("#IPADDRESS#") +
11, _message.indexOf("#PORT#"));//請求者的ip地址
int port = (Integer.valueOf(_message.substring(_message.lastIndexOf(
"#PORT#") + 6, _message.lastIndexOf("#FILE_LENGTH#")))).intValue();//端口號
String fileLength = _message.substring(_message.lastIndexOf(
"#FILE_LENGTH#") + 13);//發送文件的長度
File file = new File(_toSendFile);
try {
fb = new FileDisposeButton(_requestPeople, _toSendFile, _ipAddress,
port, fileLength);//文件處理按鈕對象
this.saveFileDisposeButton.put(_requestPeople + _toSendFile, fb);//將該對象保存進hashtable
style.addStylesToDocument(fileInfoDoc, fb);//添加樣式
fileInfoDoc.insertString(fileInfoDoc.getLength(), " " + "\n",
fileInfoDoc.getStyle("jpanel"));//把文件處理按鈕對象顯示在文件信息面板
fileInfo.validate();//激活刷新顯示
this.tablePane.setSelectedComponent(filejscrollPane);//使文件面板為當前顯示面板
}
catch (BadLocationException e) {
e.printStackTrace();
}
if (tablePane.getSelectedIndex() != 2) {//文件面板出現提示圖片
tablePane.setIconAt(2, fileTableNew);
}
}
else if (_message.startsWith("CANCEL_RECEIVED:")) {//取消接收
String _toPeople = _message.substring(_message.indexOf(
"CANCEL_RECEIVED:") + 16, _message.indexOf("#FILENAME#"));//取得該發送對象
String _fileName = _message.substring(_message.indexOf("#FILENAME#") +
10);//取得發送文件
String key = _toPeople + _fileName;//取得用于保存發送方的“取消”對象的hashtable的key
File file = new File(_fileName);//重構文件對象
FileCancelButton fileCancelButton = (FileCancelButton)
FileCancelButton.saveFileCancel.get(key);//取得hashtable當中相應的對象
fileCancelButton.fileInfo.setText(_toPeople +
constant.getLang("GG_qxwj") +
file.getName() +
constant.getLang("GG_dfs"));//設置提示信息
fileCancelButton.cancelSendButton.setVisible(false);//設置“取消”按鈕為不可見
FileCancelButton.saveFileCancel.remove(fileCancelButton);//從hashtable中移除相應的對象
}
else if (_message.startsWith("CANCEL_SENDING:")) {//取消發送
String _toPeople = _message.substring(_message.indexOf(
"CANCEL_SENDING:") + 15, _message.indexOf("#FILENAME#"));//取得“發送文件請求方”對象
String _fileName = _message.substring(_message.indexOf("#FILENAME#") +
10);//取得發送的文件
String key = _toPeople + _fileName;//取得key
File file = new File(_fileName);//重構發送文件對象
FileDisposeButton fileDisposeButton = (FileDisposeButton)
FileDisposeButton.saveFileDisposeButton.get(key);//取得相應的文件處理按鈕對象
fileDisposeButton.fileInfo.setText(_toPeople +
constant.getLang("GG_qxwj") +
file.getName() +
constant.getLang("GG_dfs"));//設置提示信息
fileDisposeButton.buttonJPanel.setVisible(false);//設置“保存”“另存為”“拒絕”按鈕為不可見
FileDisposeButton.saveFileDisposeButton.remove(fileDisposeButton);//從hashtable中移除該對象
}
else if (_message.startsWith("CONSOLE")) {//控制臺消息
MessageDialog messageDialog = new MessageDialog(new JFrame(),
_message);//控制臺消息提示對話框
messageDialog.setVisible(true);
centerOnScreen(messageDialog);
}
else if (_message.startsWith("PEOPLE_LEAVE:")) {//用戶離開
String _peopleName = _message.substring(_message.indexOf(
"PEOPLE_LEAVE:") + 13);
publicDoc.insertString(publicDoc.getLength(), _peopleName + "離開\n",
publicDoc.getStyle("regular"));//公共區顯示用戶離開信息
model.removeElement(_peopleName);
peopleList.validate();
peopleTable.remove(_peopleName);//從hashtable中移除該對象
}
Thread.sleep(5);
}
catch (Exception e) {
}
}
}
public void stop() {//用戶離開,線程停止
try {
this.socket.close();
this.out.flush();
this.out.close();
this.in.close();
}
catch (Exception e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
ChatArea chatarea = new ChatArea(new Hashtable());
JFrame f = new JFrame();
f.getContentPane().add(chatarea);
f.setVisible(true);
}
/**
*
* <p>Title:菜單和發送按鈕事件內部類 </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class Action
implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendMessageButton) {//發送按鈕事件
String peoples = "";
try {
String content = messageEditor.getMessage().trim();//取得發送消息
if (content.length() > 0 && selectNameSB.length() > 0) {
messageEditor.setMessage();
messageEditor.messageTextPane.setFocusable(true);
if (toPeople.contains("大家")) {//如果發送對象包含大家,則消息將發送給大家
StringReader stringReader = new StringReader(
"<p><font color=\"#0080C0\">^您對大家說:^</font></p>" + content);
editorKit.read(stringReader, publicDoc, publicDoc.getLength());//公共區顯示發送的消息
if (CONSOLESTATE == 1) {//用戶選擇發送控制臺消息
out.writeUTF("CONSOLE_PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" +
content);//通過套接輸出流來輸出消息
}
else {//一般消息
out.writeUTF("PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" + content);
}
}
else {//私人消息
Enumeration enum = toPeople.elements();
while (enum.hasMoreElements()) {//循環取得發送對象,然后構照發送對象字符串
peoples = peoples + enum.nextElement() + "$";//以“$”作為分割符號
}
StringReader stringReader = new StringReader(
"<p><font color=\"#0080C0\">^您對" + selectNameSB +
"說:^</font></p>" + content);
editorKit.read(stringReader, privateDoc, privateDoc.getLength());//私人區顯示發送消息
if (CONSOLESTATE == 1) {//用戶選擇控制臺消息
out.writeUTF("CONSOLE_PRIVATE_MESSAGE:" + "#TOPEOPLE#" +
peoples + "#MESSAGECONTENT#" + content);
}
else {//普通消息
out.writeUTF("PRIVATE_MESSAGE:" + "#TOPEOPLE#" + peoples +
"#MESSAGECONTENT#" + content);
}
}
}
else {
if (selectNameSB.length() <= 0) {//沒有選擇發送對象
JOptionPane.showMessageDialog(null,
constant.getLang("CA_qxxzfsdx"),
constant.getLang("CA_tsxx"),
JOptionPane.WARNING_MESSAGE);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -