📄 mainframe.java
字号:
selfId, ((Department) tempData).getDeptId(), data);
System.out.println(chatPacket);
} else {
// 如果是全公司,发公聊包,对全公司的
chatPacket = new DataPacket(DataPacket.publicChat_type,
selfId, "", data);
}
try {
oos.writeObject(chatPacket);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
Box boxBtn2 = Box.createHorizontalBox();
boxBtn2.add(Box.createHorizontalStrut(15));
boxBtn2.add(btnRecord);
boxBtn2.add(Box.createHorizontalGlue());
boxBtn2.add(btnClose);
boxBtn2.add(Box.createHorizontalStrut(20));
boxBtn2.add(btnSend);
bLeftBotBottom = new JPanel();
bLeftBotBottom.setLayout(new BorderLayout());
bLeftBotBottom.setBackground(Color.GRAY);
bLeftBotBottom.add(boxBtn2, BorderLayout.CENTER);
bLeftBotBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
bLeftBottom = new JPanel();
bLeftBottom.setLayout(new BorderLayout());
bLeftBottom.add(bLeftBotTop, BorderLayout.NORTH);
bLeftBottom.add(bLeftBotCenter, BorderLayout.CENTER);
bLeftBottom.add(bLeftBotBottom, BorderLayout.SOUTH);
bLeft.setTopComponent(bLeftTop);
bLeft.setBottomComponent(bLeftBottom);
bLeft.setDividerLocation(280);
// 绘制公告牌
jtp = new JTabbedPane();// 分页
JPanel newsPanel = new JPanel();
newsPanel.setLayout(new BorderLayout());
newsText.setText("公告在这里");
newsText.setForeground(Color.RED);
JPanel transPanel = new JPanel();
//transPanel.setLayout(new BorderLayout());
transPanel.add(Public.myprogressbar);
transPanel.add(Public.lblrate);
transPanel.add(Public.btnPause);
transPanel.add(Public.btnStop);
Public.btnPause.addActionListener(new AbstractAction(){
/**
*
*/
private static final long serialVersionUID = 4891902813491493083L;
public void actionPerformed(ActionEvent e) {
if (Public.btnPause.getText().equals("暂停")){
if (RecieveThread.getFt() != null){
RecieveThread.getFt().close();
}
if (RecieveThread.getRf() != null){
RecieveThread.getRf().close();
}
Public.btnPause.setText("继续");
}else if (Public.btnPause.getText().equals("继续")){
if (RecieveThread.getFt() != null){
RecieveThread.getFt().start();
}
if (RecieveThread.getRf() != null){
RecieveThread.getRf().start();
}
Public.btnPause.setText("暂停");
}
}
});
Public.btnStop.addActionListener(new AbstractAction(){
/**
*
*/
private static final long serialVersionUID = 1139215806212311746L;
public void actionPerformed(ActionEvent e) {
if (RecieveThread.getFt() != null){
RecieveThread.getFt().interrupt();
}
if (RecieveThread.getRf() != null){
RecieveThread.getRf().interrupt();
}
Public.btnStop.setEnabled(false);
}
});
Public.myprogressbar.setBackground(Color.WHITE);
newsPanel.add(newsText);
jtp.add("公司公告", newsPanel);
jtp.add("传输文件",transPanel);
Public.myprogressbar.setValue(50);
bRightTop = new JPanel();// 公告面板
bRightTop.setLayout(new BorderLayout());
bRightTop.add(jtp);
// 从公共变量中取得数据,生成树
Vector deptVector = Public.deptVector;
Iterator iter = deptVector.iterator();
String[] deptName = new String[deptVector.size()];
String[] deptId = new String[deptVector.size()];
int i = 0;
while (iter.hasNext()) {
Vector v = (Vector) iter.next();
deptId[i] = (String) v.get(0);
deptName[i++] = (String) v.get(1);
}
Vector userVector = Public.userVector;
Iterator userIter = userVector.iterator();
String[] userId = new String[userVector.size()];
String[] imagePath = new String[userVector.size()];
String[] userName = new String[userVector.size()];
String[] userDeptId = new String[userVector.size()];
int[] userOnLine = new int[userVector.size()];
int j = 0;
while (userIter.hasNext()) {
Vector v = (Vector) userIter.next();
userId[j] = (String) v.get(0);
imagePath[j] = (String) v.get(5);
userName[j] = (String) v.get(3);
userDeptId[j] = (String) v.get(7);
userOnLine[j] = Integer.parseInt((String) v.get(13));
if (userId[j].equals(selfId)) {
image = new ImageIcon(imagePath[j]);
imgLabel = new JLabel(userName[j], image, JLabel.LEFT);
// 存用户自己的名字
selfName = userName[j];
}
j++;
}
Box centerBox = Box.createHorizontalBox();
centerBox.add(imgLabel);
centerBox.add(Box.createHorizontalStrut(450));
centerBox.add(btnSet);
top.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
top.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
top.add(Box.createHorizontalStrut(10), BorderLayout.EAST);
top.add(centerBox, BorderLayout.CENTER);
// 生成树结构
root = new DefaultMutableTreeNode("百度公司");// 根节点
treeModel = new DefaultTreeModel(root);
DefaultMutableTreeNode[] branch = new DefaultMutableTreeNode[deptVector
.size()];// 子节点
// 叶子节点
for (int i1 = 0; i1 < deptVector.size(); i1++) {
// 部门信息来填充节点
Department dept = new Department(deptId[i1], deptName[i1]);
branch[i1] = new DefaultMutableTreeNode(dept);
/*
* 利用DefaultTreeModel类所提供的insertNodeInto()方法加入节点到父节点,
* 其中DefaultTreeModel类所提供的getChildCount()方法取得目前子节点的个数
*/
treeModel.insertNodeInto(branch[i1], root, root.getChildCount());
// for循环插入叶子节点,对应的每个部门下的用户的头像
for (int j1 = 0; j1 < userVector.size(); j1++) {
// 判断这个用户是否在这个部门
if (deptId[i1].equals(userDeptId[j1])) {
User user = new User(userId[j1], userName[j1],
imagePath[j1], userOnLine[j1]);
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(
user);
if (user.isOnline() == 1) {
treeModel.insertNodeInto(leaf, branch[i1], 0);// 放在树的节点的第一位
} else {
treeModel.insertNodeInto(leaf, branch[i1], branch[i1]
.getChildCount());// 放在树的节点的第一位
}
Public.userTable.put(userId[j1], leaf);// 将树节点存放在公共变量里
}
}
}
// 创建一棵TreeModel模式的树,以TreeModel建立树
tree = new JTree(treeModel);
tree.setCellRenderer(new MyTreeCellRenderer());
//全展开树
for (int i1 = 0;i1 < tree.getRowCount();i1++) {
tree.expandRow(i1);
}
// 当用点击 JTree 时产生的事件
tree.getSelectionModel().addTreeSelectionListener(
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
Object o = ((DefaultMutableTreeNode) MainFrame.this.tree
.getLastSelectedPathComponent())
.getUserObject();
if (o instanceof User) {
User user = (User) o;
// 判断是不是自己
if (user.getId().equals(selfId)) {
JOptionPane.showMessageDialog(f, "不能给自己发消息!",
"提示框", JOptionPane.WARNING_MESSAGE);
} else {
if (user.isOnline() == 1) {
// 将对话的用户的信息显示在面板上
String iconPath = user.getIcon().substring(
0, user.getIcon().length() - 4);
iconPath = iconPath + "_m.gif";
toUser.setText(user.getName());
toUser.setIcon(new ImageIcon(iconPath));
// 将用户的信息存放在一个临时的地方
tempData = user;
objuser = user;
} else {
JOptionPane.showMessageDialog(f,
"不能给不在线用户发消息!", "提示框",
JOptionPane.WARNING_MESSAGE);
}
}
} else if (o instanceof Department) {
Department dept = (Department) o;
// toUser.setIcon(new ImageIcon(
// "./Image/total/department.gif"));
toUser.setIcon(QQUtils
.getTotalIcon("department.gif"));
toUser.setText(dept.getDeptName());
tempData = dept;
// JOptionPane.showMessageDialog(f,
// "该部门没有用户在线,不能向该部门发消息!", "提示框",
// JOptionPane.WARNING_MESSAGE);
} else {
toUser.setText("");
// toUser.setIcon(new ImageIcon(
// "./Image/total/company.gif"));
toUser.setIcon(QQUtils.getTotalIcon("company.gif"));
}
}
});
JScrollPane scrollPane3 = new JScrollPane(tree);
bRightBottom = new JPanel();
bRightBottom.setLayout(new BorderLayout());
bRightBottom.add(scrollPane3);
// 右边面版的布局
bRight.setTopComponent(bRightTop);
bRight.setBottomComponent(bRightBottom);
bRight.setDividerLocation(150);
// 组合所有构件
c.add(top, BorderLayout.NORTH);
c.add(bottom, BorderLayout.CENTER);
// 移动到按钮时变化图片
btnFont.setRolloverIcon((ImageIcon) QQUtils
.getTotalIcon("setfontover.gif"));
btnFace.setRolloverIcon((ImageIcon) QQUtils
.getTotalIcon("setfaceover.gif"));
btnSendImg.setRolloverIcon((ImageIcon) QQUtils
.getTotalIcon("sendimgover.gif"));
btnSendFile.setRolloverIcon((ImageIcon) QQUtils
.getTotalIcon("sendfileover.gif"));
}
private void sendToServer() {
// 给服务器端发下线包
selfSocketList = (ArrayList) Public.mySocketInfo.get(selfId);
oos = (ObjectOutputStream) selfSocketList.get(2);
// 封装下线包
DataPacket dataPacket = new DataPacket(DataPacket.down_line_type,
selfId, "", null);
if (dataPacket != null) {
try {
oos.writeObject(dataPacket);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
* 发送消息框模型.
*/
class MyModel extends DefaultStyledDocument {
private static final long serialVersionUID = -5562302113895445987L;
public void insertString(int offs, String str, AttributeSet attr)
throws BadLocationException {
if (attr.toString().indexOf("icon") > -1) {
super.insertString(offs, str, attr);
} else {
MutableAttributeSet attrSet = QQUtils.getFontAttr();
super.insertString(offs, str, attrSet);
}
}
}
public JTextPane getTextContent() {
return TextContent;
}
public void setTextContent(JTextPane textContent) {
TextContent = textContent;
}
public JTextPane getNewsText() {
return newsText;
}
public String getSelfId() {
return selfId;
}
public JTabbedPane getJtp() {
return jtp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -