📄 mainframe.java
字号:
package com.zlf.qqclient.mainframe;
/**
*客户端聊天界面,生成树
*@author zlf
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import com.zlf.qq.pub.DataPacket;
import com.zlf.qqclient.filetransfer.MyFileChooser;
import com.zlf.qqclient.login.RecieveThread;
import com.zlf.qqclient.mainframe.facedialog.FaceDialog;
import com.zlf.qqclient.mainframe.tree.Department;
import com.zlf.qqclient.mainframe.tree.MyTreeCellRenderer;
import com.zlf.qqclient.mainframe.tree.User;
import com.zlf.qqclient.mainframe.updatepwd.updatePwdDialog;
import com.zlf.qqclient.utils.Public;
import com.zlf.qqclient.utils.QQUtils;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel top;// 上边放标签和按钮
private JSplitPane bottom;// 下边又分两边,左边和右边
private JSplitPane bLeft;// 左边又分上下
private JSplitPane bRight;// 右边又分上下
private JPanel bLeftTop;// 显示区
private JPanel bLeftTopTop;// 对谁说的标题栏
private JLabel toLabel;// to
private JLabel toUser;// //显示对话的人的头像
private JPanel bLeftTopCenter;
private JPanel bLeftTopBottom;// 字体设置的面板
private JPanel bLeftBottom;// 发送区
private JPanel bLeftBotTop;// 按钮栏
private JPanel bLeftBotCenter;// 编辑区
private JPanel bLeftBotBottom;// 发送区
private JPanel bRightTop;// 公告区
private JPanel bRightBottom;// 树,用户显示
private JLabel imgLabel;// 用户大头像的标签
private ImageIcon image;
private JButton btnSet;// 按钮
private JTextPane TextContent;// 内容显示
private JButton btnFont;// 设置字体
private JButton btnFace;// 设置表情
private JButton btnSendImg;// 发送图片
private JButton btnSendFile;// 发送文件
private JTextPane TextSendContent;// 编辑区
private JButton btnRecord;// 聊天记录 按钮
private JButton btnClose;// 关闭
private JButton btnSend;// 发送
private JTabbedPane jtp;
private JTree tree;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode root;
private String selfId;// QQ号
private String selfName;// 名字
private JTextPane newsText = new JTextPane();// 公告牌
// 通讯变量
private ArrayList selfSocketList = null;
private ObjectOutputStream oos = null;
private Object tempData = null;
private User objuser = null;
public DefaultTreeModel getTreeModel() {
return treeModel;
}
public void setTreeModel(DefaultTreeModel treeModel) {
this.treeModel = treeModel;
}
public JTree getTree() {
return tree;
}
public void setTree(JTree tree) {
this.tree = tree;
}
public MainFrame(final String userId) {
super("聊天主窗口");
this.selfId = userId;
ini(this);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// 向服务器发下线包
sendToServer();
}
});
}
// 初始化主界面
private void ini(final MainFrame f) {
this.setSize(700, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
top = new JPanel();
top.setBackground(Color.gray);
top.setLayout(new BorderLayout());
// 用户设置按钮
ImageIcon btnImg = (ImageIcon) QQUtils.getTotalIcon("seticon.gif");
btnSet = new JButton(btnImg);
btnSet.setBorder(BorderFactory.createEmptyBorder());
btnSet.setBackground(Color.GRAY);
btnSet.setContentAreaFilled(false);
btnSet.addActionListener(new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = -8015214834919246070L;
public void actionPerformed(ActionEvent e) {
// 弹出设置用户密码的对话框
new updatePwdDialog(selfId).setVisible(true);
}
});
bottom = new JSplitPane();
bottom.setDividerLocation(500);
bLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT);// 上下分的
bLeft.setBorder(BorderFactory.createLineBorder(Color.black, 1));
bLeft.setDividerSize(2);
bRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
bRight.setBorder(BorderFactory.createLineBorder(Color.black, 1));
bottom.setLeftComponent(bLeft);
bottom.setRightComponent(bRight);
// 显示对方的头像
bLeftTop = new JPanel();
bLeftTopTop = new JPanel();
bLeftTopTop.setBackground(new Color(230, 250, 250));
bLeftTopTop.setLayout(new FlowLayout(FlowLayout.LEFT));
toLabel = new JLabel("To:");
toUser = new JLabel((ImageIcon) QQUtils.getTotalIcon("company.gif"));
bLeftTopTop.add(toLabel);
bLeftTopTop.add(toUser);
bLeftTopTop.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 0));
bLeftTopCenter = new JPanel();
// 显示聊天面板
TextContent = new JTextPane();
TextContent.setEditable(false);
JScrollPane scrollPane = new JScrollPane(TextContent);
bLeftTopCenter.setLayout(new BorderLayout());
bLeftTopCenter.add(scrollPane);
// 字体设置面板,起初隐藏着
bLeftTopBottom = new JPanel(new FlowLayout(FlowLayout.LEFT));
// 字体设置
// 返回一个包含此GraphicsEnvironment中所有字体系列名称的数组,它针对默认语言环境进行了本地化,由
// Locale.getDefault() 返回。
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
String fontStrs[] = ge.getAvailableFontFamilyNames();
JComboBox fontCombo = new JComboBox(fontStrs);
fontCombo.setBackground(Color.white);
fontCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// 设置字体
QQUtils.properties.setProperty("DefaultFont", e.getItem()
.toString());
// 取得文本的样式
DefaultStyledDocument sd = (DefaultStyledDocument) TextSendContent
.getStyledDocument();
// 将在发送区编辑的文本,或图片存入ArrayList中
ArrayList al = new ArrayList();
for (int i = 0; i < sd.getLength(); i++) {
// 获取表示文档中给定偏移量处字符的元素。 偏移量 >= 0
Element element = sd.getCharacterElement(i);
// element.getName()获取元素的名称。如果该元素用于表示某些类型的结构,这将是类型名。
if (element.getName().equals("icon")) {
// element.getAttributes()获取此元素包含的属性集合。
Icon icon = StyleConstants.getIcon(element
.getAttributes());
al.add(icon);
} else if (element.getName().equals("content")) {
try {
al.add(TextSendContent.getText(i, 1));
} catch (BadLocationException e1) {
e1.printStackTrace();
}
} else if (element.getName().equals("component")) {
JOptionPane.showMessageDialog(null, "component");
}
SimpleAttributeSet sas = new SimpleAttributeSet();// 属性集合
StyleConstants.setForeground(sas, Color.BLUE);// 设置字体前景色
}
}
});
bLeftTopBottom.add(fontCombo);
JComboBox sizeCombo = new JComboBox();
for (int i = 8; i < 48; i = i + 2) {
sizeCombo.addItem(String.valueOf(i));
}
sizeCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
QQUtils.properties.setProperty("DefaultFontSize", e.getItem()
.toString());
}
});
sizeCombo.setBackground(Color.white);
bLeftTopBottom.add(sizeCombo);
JButton btnBord = new JButton((ImageIcon) QQUtils
.getFontIcon("fontb.gif"));
btnBord.setBorder(BorderFactory.createEmptyBorder());
// 设置粗体
btnBord.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (QQUtils.properties.getProperty("bold").equalsIgnoreCase(
"yes")) {
QQUtils.properties.setProperty("bold", "no");
} else {
QQUtils.properties.setProperty("bold", "yes");
}
}
});
bLeftTopBottom.add(btnBord);
JButton btnI = new JButton((ImageIcon) QQUtils.getFontIcon("fonti.gif"));
btnI.setBorder(BorderFactory.createEmptyBorder());
// 设置斜体
btnI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (QQUtils.properties.getProperty("Italic").equalsIgnoreCase(
"yes")) {
QQUtils.properties.setProperty("Italic", "no");
} else {
QQUtils.properties.setProperty("Italic", "yes");
}
}
});
bLeftTopBottom.add(btnI);
JButton btnUnderLine = new JButton((ImageIcon) QQUtils
.getFontIcon("fontu.gif"));
btnUnderLine.setBorder(BorderFactory.createEmptyBorder());
btnUnderLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (QQUtils.properties.getProperty("UnderLine")
.equalsIgnoreCase("yes")) {
QQUtils.properties.setProperty("UnderLine", "no");
} else {
QQUtils.properties.setProperty("UnderLine", "yes");
}
}
});
bLeftTopBottom.add(btnUnderLine);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -