?? chatarea.java~970~
字號:
package chat;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.text.*;
import java.net.*;
import javax.swing.JTabbedPane;
import javax.swing.text.html.*;
/**
*
* <p>Title: 客戶端主界面</p>
* <p>Description:客戶端主界面,包括消息編輯,消息查看,文件發(fā)送,文件接收,在線列表 </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ChatArea extends JPanel implements Runnable{
//聲明Constant映射類對象
Constant constant = new Constant();
BorderLayout borderLayout1 = new BorderLayout();
Action action = new Action();
//自動回復(fù)菜單事件對象
RevertAction revertAction = new RevertAction();
//控制臺菜單事件對象
ConsoleAction consoleAction = new ConsoleAction();
//聊天記錄菜單事件對象
ChatLogAction chatLogAction = new ChatLogAction();
//消息編輯器
EditorMessage messageEditor = new EditorMessage();
TablePaneAction tablePaneAction = new TablePaneAction();
HTMLEditorKit editorKit = new HTMLEditorKit();
String userHome = System.getProperty("user.home");
//消息編輯器面板背景顏色
Color bgcolor = new Color(190, 212, 255);
//自定義邊框類對象
OvalBorder ovalBorder = new OvalBorder(5, 5, new Color(0, 153, 255),
new Color(0, 153, 255));
//自定義樣式類對象,用于文件提示信息面板樣式控制
static Styles style = new Styles();
static Font font = new Font("新宋體", Font.PLAIN, 12);
//自動回復(fù)控制變量
static private int REVERTOPTION = 0;
//自動回復(fù)信息
static private String REVERTMESSAGE = null;
//控制臺消息
static private int CONSOLESTATE = 0;
//聊天記錄文件對象
File chatLogFile;
FileInputStream chatLogIn;
FileReader chatLogFileReader;
BufferedReader chatLogBR;
//圖片路徑
String toolbarImagePath = "/images/toolbar/";
ImageIcon sendButtonnormalIcon = new ImageIcon(getClass().getResource(
toolbarImagePath + "sendmesagenormal.png"));
ImageIcon sendButtonovalIcon = new ImageIcon(getClass().getResource(
toolbarImagePath + "sendmessageoval.png"));
ImageIcon publicTableIcon = new ImageIcon(getClass().getResource(
toolbarImagePath +"publictable.png"));
ImageIcon publicTableNew = new ImageIcon(getClass().getResource(
toolbarImagePath + "publictablenew.gif"));
ImageIcon privateTableIcon = new ImageIcon(getClass().getResource(
toolbarImagePath + "privatetable.png"));
ImageIcon privateTableNew = new ImageIcon(getClass().getResource(
toolbarImagePath + "privatetablenew.gif"));
ImageIcon fileTableIcon = new ImageIcon(getClass().getResource(
toolbarImagePath + "filetable.png"));
ImageIcon fileTableNew = new ImageIcon(getClass().getResource(
toolbarImagePath + "filetablenew.gif"));
ImageIcon chatLogIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
"chatLog.png"));
ImageIcon chatLogIconNew = new ImageIcon(getClass().getResource(
toolbarImagePath + "chatLognew.gif"));
//發(fā)送文件處理按鈕對象
FileDisposeButton fb;
//登錄對話框?qū)ο? static LoginDialog loginDialog = new LoginDialog();
//文件選擇器
final JFileChooser chooser = new JFileChooser();
JFontChooser fontChooser = new JFontChooser(new JFrame());
JButton jButton1 = new JButton();
//菜單條
JMenuBar jmenuBar = new JMenuBar();
JMenu loginMenu = new JMenu();
JMenu fileMenu = new JMenu();
JMenuItem loginOut = new JMenuItem();
JMenuItem sendFile = new JMenuItem();
//自動回復(fù)菜單和菜單項
ButtonGroup revertGroup = new ButtonGroup();
JMenu revertMenu = new JMenu();
JCheckBoxMenuItem gongzuo = new JCheckBoxMenuItem();
JCheckBoxMenuItem chifan = new JCheckBoxMenuItem();
JCheckBoxMenuItem youshi = new JCheckBoxMenuItem();
JCheckBoxMenuItem zidingyi = new JCheckBoxMenuItem();
JCheckBoxMenuItem quxiao = new JCheckBoxMenuItem();
//控制臺菜單和菜單項
JMenu consoleMenu = new JMenu();
JRadioButtonMenuItem consoleMenuItem = new JRadioButtonMenuItem();
//聊天記錄菜單和菜單項
ButtonGroup chatLogGroup = new ButtonGroup();
JMenu chatLogMenu = new JMenu();
JCheckBoxMenuItem chatLogPriMenuItem = new JCheckBoxMenuItem();
JCheckBoxMenuItem chatLogPubMenuItem = new JCheckBoxMenuItem();
JCheckBoxMenuItem chatLogCloseMenuItem = new JCheckBoxMenuItem();
//存儲文件處理按鈕對象的Hashtable
static Hashtable saveFileDisposeButton = new Hashtable();
//公共、私人、文件提示信息組件
static JTextPane publicChat = new JTextPane();
static JTextPane privateChat = new JTextPane();
static JTextPane fileInfo = new JTextPane();
//聊天記錄顯示組件
JTextArea chatLog = new JTextArea();
static JTabbedPane tablePane = new JTabbedPane();
//取得公共和私人消息顯示組件的document對象
static Document publicChatDocument = publicChat.getDocument();
static Document privateChatDocument = privateChat.getDocument();
static MutableAttributeSet char_style = new SimpleAttributeSet();
HTMLDocument privateDoc;
HTMLDocument publicDoc;
static StyledDocument fileInfoDoc = fileInfo.getStyledDocument();
//聲明DefaultListModel用于用戶列表動態(tài)添加數(shù)據(jù)
DefaultListModel model = new DefaultListModel();
JList peopleList = new JList(model);
//聲明用于放置公共、私人、文件提示信息、在線用戶和聊天記錄的滾動面板
JScrollPane publicjscrollPane = new JScrollPane(publicChat);
JScrollPane privatejscrollPane = new JScrollPane(privateChat);
JScrollPane filejscrollPane = new JScrollPane(fileInfo);
JScrollPane listPane = new JScrollPane(peopleList);
JScrollPane chatLogscrollPane = new JScrollPane(chatLog);
JSplitPane jsplitPane1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
publicjscrollPane, privatejscrollPane);
//jsplitPane2劈開面板用于放置現(xiàn)在用戶列表和tablePane
JSplitPane jsplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
listPane, tablePane);
JPanel jpanel1 = new JPanel();
JPanel jpanel2 = new JPanel();
JPanel jpanel3 = new JPanel();
JPanel jpanel4 = new JPanel();
JPanel jpanel5 = new JPanel();
//發(fā)送消息按鈕
JButton sendMessageButton = new JButton(sendButtonnormalIcon);
JButton clearSendObjectButton = new JButton();
//發(fā)送消息或文件對象顯示標(biāo)簽
static JLabel sendObject = new JLabel(" ");
File file = null;
//存儲在先用戶列表的hashtable
static Hashtable peopleTable = new Hashtable();
//輸入、輸出流對象
static DataOutputStream out = null;
DataInputStream in = null;
//連接服務(wù)器的socket對象(發(fā)送消息用)
static Socket socket = null;
Thread messageThread = null;
//存儲發(fā)送對象的向量對象
Vector toPeople = new Vector();
String selectName = "";
//存儲發(fā)送對象的Stringbuffer對象
StringBuffer selectNameSB = new StringBuffer();
String userName;
public ChatArea(Hashtable peopleTable) {
this.peopleTable = peopleTable;
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
//設(shè)置系統(tǒng)默認(rèn)的字體
UIManager.put("Label.font", font);
UIManager.put("List.font", font);
UIManager.put("TabbedPane.font", font);
UIManager.put("MenuBar.font", font);
UIManager.put("Menu.font", font);
UIManager.put("MenuItem.font", font);
UIManager.put("PopupMenu.font", font);
UIManager.put("CheckBoxMenuItem.font", font);
UIManager.put("RadioButtonMenuItem.font", font);
UIManager.put("OptionPane.messageFont", font);
UIManager.put("OptionPane.buttonFont", font);
Component[] components = jmenuBar.getComponents();
for (int i = 0; i < components.length; i++) {
( (JMenu) components[i]).setFont(font);
}
//初始狀態(tài)設(shè)置
publicChat.setEditable(false);
privateChat.setEditable(false);
fileInfo.setEditable(false);
listPane.setPreferredSize(new Dimension(70, 75));
centerOnScreen(loginDialog);
centerOnScreen(fontChooser);
loginMenu.setEnabled(false);
fileMenu.setEnabled(false);
revertMenu.setEnabled(false);
consoleMenu.setEnabled(false);
chatLogMenu.setEnabled(false);
sendMessageButton.setEnabled(false);
sendMessageButton.setBackground(Color.white);
sendMessageButton.setBorder(null);
sendMessageButton.setRolloverIcon(sendButtonovalIcon);
clearSendObjectButton.setEnabled(false);
publicChat.setEditable(false);
privateChat.setEditable(false);
jButton1.setText("jButton1");
loginDialog.setVisible(true);
//設(shè)置菜單和菜單顯示文本
loginMenu.setText(constant.getLang("CA_loginMenu"));
fileMenu.setText(constant.getLang("CA_fileMenu"));
revertMenu.setText(constant.getLang("CA_revertMenu"));
loginOut.setText(constant.getLang("CA_loginOut"));
sendFile.setText(constant.getLang("CA_sendFile"));
consoleMenu.setText(constant.getLang("CA_consoleMenu"));
chatLogMenu.setText(constant.getLang("CA_chatLogMenu"));
revertGroup.add(gongzuo);
revertGroup.add(chifan);
revertGroup.add(youshi);
revertGroup.add(zidingyi);
revertGroup.add(quxiao);
chatLogGroup.add(chatLogPriMenuItem);
chatLogGroup.add(chatLogPubMenuItem);
chatLogGroup.add(chatLogCloseMenuItem);
gongzuo.setText(constant.getLang("CA_gongzuo"));
chifan.setText(constant.getLang("CA_chifan"));
youshi.setText(constant.getLang("CA_youshi"));
zidingyi.setText(constant.getLang("CA_zidingyi"));
quxiao.setText(constant.getLang("CA_quxiao"));
consoleMenuItem.setText(constant.getLang("CA_consoleMenuItem"));
chatLogPriMenuItem.setText(constant.getLang("CA_chatLogPriMenuItem"));
chatLogPubMenuItem.setText(constant.getLang("CA_chatLogPubMenuItem"));
chatLogCloseMenuItem.setText(constant.getLang("CA_chatLogCloseMenuItem"));
messageEditor.setBounds(new Rectangle(5, 5, 10, 10));
//添加鍵盤事件,處理Ctrl+Enter發(fā)送消息
messageEditor.messageTextPane.addKeyListener(new keyBoardAction());
jmenuBar.add(loginMenu);
jmenuBar.add(fileMenu);
jmenuBar.add(revertMenu);
jmenuBar.add(consoleMenu);
jmenuBar.add(chatLogMenu);
loginMenu.add(loginOut);
fileMenu.add(sendFile);
revertMenu.add(gongzuo);
revertMenu.add(chifan);
revertMenu.add(youshi);
revertMenu.add(zidingyi);
revertMenu.add(quxiao);
consoleMenu.add(consoleMenuItem);
chatLogMenu.add(chatLogPriMenuItem);
chatLogMenu.add(chatLogPubMenuItem);
chatLogMenu.add(chatLogCloseMenuItem);
publicjscrollPane.setBorder(ovalBorder);
publicjscrollPane.setBackground(Color.white);
privatejscrollPane.setBorder(ovalBorder);
chatLogscrollPane.setBorder(ovalBorder);
privatejscrollPane.setBackground(Color.white);
filejscrollPane.setBorder(ovalBorder);
filejscrollPane.setBackground(new Color(140, 173, 215));
tablePane.addTab("", publicTableIcon, publicjscrollPane);
tablePane.addTab("", privateTableIcon, privatejscrollPane);
tablePane.addTab("", fileTableIcon, filejscrollPane);
tablePane.addMouseListener(tablePaneAction);
//設(shè)置劈開面板
jsplitPane1.setDividerSize(12);
jsplitPane1.setOneTouchExpandable(true);
jsplitPane2.setDividerSize(12);
jsplitPane2.setOneTouchExpandable(true);
clearSendObjectButton.setText("清除選定對像");
//設(shè)置公共和私人聊天區(qū)的處理字符的編碼格式
privateChat.setContentType("text/html");
publicChat.setContentType("text/html");
fileInfo.setBackground(new Color(140, 173, 215));
chatLog.setEditable(false);
//取得公共和私聊區(qū)的 Document 對象
privateDoc = (HTMLDocument) privateChat.getDocument();
publicDoc = (HTMLDocument) publicChat.getDocument();
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -