📄 mainframe.java
字号:
bLeftTopBottom.setBackground(Color.yellow);
bLeftTopBottom.setVisible(false);
bLeftTop.setLayout(new BorderLayout());
bLeftTop.add(bLeftTopTop, BorderLayout.NORTH);
bLeftTop.add(bLeftTopCenter, BorderLayout.CENTER);
bLeftTop.add(bLeftTopBottom, BorderLayout.SOUTH);
bLeftBotTop = new JPanel();
// 字体设置按钮
btnFont = new JButton((ImageIcon) QQUtils.getTotalIcon("setfont.gif"));
btnFont.setBorder(BorderFactory.createEmptyBorder());
btnFont.setBackground(new Color(230, 250, 250));
btnFont.setContentAreaFilled(false);
btnFont.addActionListener(new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
// 使隐藏的面板显示
bLeftTopBottom.setVisible(!bLeftTopBottom.isVisible());
}
});
// 编辑文本框
TextSendContent = new JTextPane(new MyModel());
bLeftBotCenter = new JPanel();
bLeftBotCenter.setLayout(new BorderLayout());
//System.out.println(TextSendContent.getFont());
JScrollPane scrollPane2 = new JScrollPane(TextSendContent);
bLeftBotCenter.add(scrollPane2);
// 表情按钮
btnFace = new JButton();
btnFace = new JButton((ImageIcon) QQUtils.getTotalIcon("setface.gif"));
btnFace.setBorder(BorderFactory.createEmptyBorder());
btnFace.setBackground(new Color(230, 250, 250));
btnFace.setContentAreaFilled(false);
btnFace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FaceDialog fd = new FaceDialog(TextSendContent);
Point p = btnFace.getLocationOnScreen();
fd.pack();
fd.setLocation(new Point(p.x, p.y - fd.getHeight()));
fd.setVisible(true);
}
});
// 插入图片
btnSendImg = new JButton();
btnSendImg = new JButton((ImageIcon) QQUtils
.getTotalIcon("sendimg.gif"));
btnSendImg.setBorder(BorderFactory.createEmptyBorder());
btnSendImg.setBackground(new Color(230, 250, 250));
btnSendImg.setContentAreaFilled(false);
btnSendImg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MyFileChooser fc = new MyFileChooser();
fc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()
|| f.getName().toLowerCase().endsWith(".gif")
|| f.getName().toLowerCase().endsWith(".jpg")
|| f.getName().toLowerCase().endsWith(".bmp")
|| f.getName().toLowerCase().endsWith(".jpeg")) {
return true;
} else {
JOptionPane.showMessageDialog(null, "插入的不是图像文件");
return false;
}
}
public String getDescription() {
return "图像文件(*.jpg;*.jpeg;*.bmp;*.gif;)";
}
});
StyledDocument sd = TextSendContent.getStyledDocument();
if (fc.showDialog(MainFrame.this, "选择插入的图像") == JFileChooser.APPROVE_OPTION) {
TextSendContent.setCaretPosition(sd.getLength());
ImageIcon icon = new ImageIcon(fc.getSelectedFile()
.getAbsolutePath());
TextSendContent.insertIcon(icon);
}
TextSendContent.requestFocus();
}
});
btnSendFile = new JButton();
btnSendFile = new JButton((ImageIcon) QQUtils
.getTotalIcon("sendfile.gif"));
btnSendFile.setBorder(BorderFactory.createEmptyBorder());
btnSendFile.setBackground(new Color(230, 250, 250));
btnSendFile.setContentAreaFilled(false);
btnSendFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (objuser == null){
JOptionPane.showMessageDialog(null,"请选择要发送文件的对象");
return;
}
MyFileChooser fc = new MyFileChooser();
fc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f != null)
return true;
return false;
}
public String getDescription() {
return null;
}
});
if (fc.showDialog(MainFrame.this, "选择要发送的文件") == JFileChooser.APPROVE_OPTION) {
// 发送消息包
ObjectOutputStream oos = (ObjectOutputStream) ((ArrayList) Public.mySocketInfo
.get(selfId)).get(2);
DataPacket infoPacket = null;
HashMap data = new HashMap();
data.put("filename",fc.getSelectedFile().getAbsolutePath());
infoPacket = new DataPacket(DataPacket.transfile_type,
selfId, ((User) objuser).getId(), data);
try {
oos.writeObject(infoPacket);
oos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
TextSendContent.requestFocus();
}
});
Box boxBtn = Box.createHorizontalBox();
boxBtn.add(btnFont);
boxBtn.add(Box.createHorizontalStrut(10));
boxBtn.add(btnFace);
boxBtn.add(Box.createHorizontalStrut(10));
boxBtn.add(btnSendImg);
boxBtn.add(Box.createHorizontalStrut(10));
boxBtn.add(btnSendFile);
bLeftBotTop.setLayout(new BorderLayout());
bLeftBotTop.add(boxBtn);
bLeftBotTop.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 5));
bLeftBotTop.setBackground(new Color(230, 250, 250));
// 聊天记录
btnRecord = new JButton((ImageIcon) QQUtils.getTotalIcon("set.gif"));
btnRecord.setBorder(BorderFactory.createEmptyBorder());// 去除边框
btnRecord.setText("聊天记录(H)");
btnRecord.setMnemonic('H');
btnRecord.setHorizontalTextPosition(JButton.CENTER);
btnRecord.setVerticalTextPosition(JButton.CENTER);
btnRecord.addActionListener(new AbstractAction(){
/**
*
*/
private static final long serialVersionUID = 5827429360758014314L;
public void actionPerformed(ActionEvent e) {
JFrame jframe = new JFrame();
final Container cc = jframe.getContentPane();
cc.setLayout(new BorderLayout());
JTextArea ta = new JTextArea();
ta.setText(f.getTextContent().getText());
cc.add(ta);
final Dialog dialog = new Dialog(jframe);
dialog.add(cc);
dialog.setSize(300, 200);
dialog.setLocationRelativeTo(f);
dialog.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0) {
// TODO 自动生成方法存根
}
public void windowClosing(WindowEvent arg0) {
dialog.dispose();
}
public void windowDeactivated(WindowEvent arg0) {
dialog.dispose();
}
public void windowDeiconified(WindowEvent arg0) {
// TODO 自动生成方法存根
}
public void windowIconified(WindowEvent arg0) {
// TODO 自动生成方法存根
}
public void windowOpened(WindowEvent arg0) {
}
});
dialog.show();
}
});
btnClose = new JButton((ImageIcon) QQUtils
.getTotalIcon("closebutton.gif"));
btnClose.setBorder(BorderFactory.createEmptyBorder());
btnClose.setText("关闭(C)");
btnClose.setMnemonic('C');
btnClose.setHorizontalTextPosition(JButton.CENTER);
btnClose.setVerticalTextPosition(JButton.CENTER);
// 关闭按钮执行
btnClose.addActionListener(new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = -364987352925048596L;
public void actionPerformed(ActionEvent e) {
// 给服务器发下线包
sendToServer();
System.exit(0);
}
});
// 发送按钮
btnSend = new JButton((ImageIcon) QQUtils
.getTotalIcon("sendbutton.gif"));
btnSend.setBorder(BorderFactory.createEmptyBorder());
btnSend.setText("发送(S)");
btnSend.setMnemonic('S');
btnSend.setHorizontalTextPosition(JButton.CENTER);
btnSend.setVerticalTextPosition(JButton.CENTER);
btnSend.addActionListener(new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = -4190373256589208017L;
public void actionPerformed(ActionEvent e) {
if (TextSendContent.getText().equals("")){
JOptionPane.showMessageDialog(null,"不能发送空消息");
return;
}
// 取得文本的样式
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")) {
}
}
// 清空编辑区
TextSendContent.setText("");
TextContent.setEditable(true);
// 获取与编辑器关联的模型。StyledDocument用作通用样式化文档的接口。
StyledDocument sdShow = TextContent.getStyledDocument();
SimpleAttributeSet sas = new SimpleAttributeSet();// 属性集合
StyleConstants.setForeground(sas, Color.BLUE);// 设置字体前景色
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
// 返回文档中当前内容的字符数。
int lenShow = sdShow.getLength();
// 设置发送的字体样式
MutableAttributeSet attrSet = QQUtils.getFontAttr();
try {
// 在显示区填加内容
sdShow.insertString(lenShow, selfName + " " + date + "\n",
sas);
for (int i = 0; i < al.size(); i++) {
// 返回文档中当前内容的字符数。
lenShow = sdShow.getLength();
// 设置 TextComponent 的文本插入符的位置。
TextContent.setCaretPosition(lenShow);
Object o = al.get(i);
if (o instanceof Icon) {
//System.out.println((Icon) o);
TextContent.insertIcon((ImageIcon) o);// 将图片显示在面板上
} else if (o instanceof String) {
sdShow.insertString(lenShow, (String) o, attrSet);
}
}
lenShow = sdShow.getLength();
sdShow.insertString(lenShow, "\n", null);
TextContent.setEditable(false);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
TextSendContent.requestFocus();
// 发送聊天包
ObjectOutputStream oos = (ObjectOutputStream) ((ArrayList) Public.mySocketInfo
.get(selfId)).get(2);
DataPacket chatPacket = null;
HashMap data = new HashMap();
data.put("data", al);
data.put("font", attrSet);
if (tempData instanceof User) {
// 如果是用户,发私聊包
chatPacket = new DataPacket(DataPacket.privateChat_type,
selfId, ((User) tempData).getId(), data);
} else if (tempData instanceof Department) {
// 如果是部门,发公聊包
chatPacket = new DataPacket(DataPacket.deptChat_type,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -