📄 fontpanel.java
字号:
package com.softfz.jn0708.chat.sendpanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import com.softfz.jn0708.chat.chatcontent.ChatContent;
import com.softfz.jn0708.chat.sendpanel.fontpanel.colorcombobox.ColorComboBox;
import com.softfz.jn0708.chat.sendpanel.fontpanel.fontcombobox.FontComboBox;
import com.softfz.jn0708.chat.sendpanel.fontpanel.fontsizecombobox.FontSizeComboBox;
import com.softfz.jn0708.images.Images;
import com.softfz.jn0708.srcpackage.FilePath;
import com.softfz.jn0708.srcpackage.SrcPackage;
/**
* 字体选择面板
*/
public class FontPanel extends JPanel implements ItemListener {
private FontComboBox cbFont = null;
private ColorComboBox cbColor = null;
private FontSizeComboBox cbFontSize = null;
private JTextPane jtpSend = null;
private JTextPane jtpSend2 = null;
private SrcPackage sendfilePackage = null;
private final JButton fileSendbtn = new JButton();
private final JButton picSendbtn = new JButton();
private ChatContent chatcontent = null;
private SendPanel sendpanel = null;
/**
* 字体面板构造
* @param chatcontent 聊天内容面板
* @param sendpanel 发送面板
*/
public FontPanel(final ChatContent chatcontent,final SendPanel sendpanel) {
super();
this.chatcontent = chatcontent;
this.sendpanel = sendpanel;
JTextPane jtpSend = chatcontent.getJtpchatcontent();
JTextPane jtpSend2 = sendpanel.getJtpininfo();
this.jtpSend = jtpSend;
this.jtpSend2 = jtpSend2;
this.setMaximumSize(new Dimension(880,21));
this.setPreferredSize(new Dimension(347,21));
this.setBackground(new Color(240,245,251));
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
final JButton facebtn = new JButton();
facebtn.setActionCommand("face");
URL url = Images.class.getResource("faceIcon/0.gif");
if (url != null){
ImageIcon icon = new ImageIcon(url);
if(icon != null){
facebtn.setIcon(icon);
}
}
/**
* 按钮监听
*/
facebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
FaceDialog facedialog = new FaceDialog(chatcontent,sendpanel);
Point p = facebtn.getLocationOnScreen();
facedialog.pack();
facedialog.setLocation(new Point(p.x, p.y - facedialog.getHeight()));
facedialog.show();
}
});
facebtn.setPreferredSize(new Dimension(26,21));
fileSendbtn.setActionCommand("SENDFILE");
url = Images.class.getResource("faceIcon/sendfile0.gif");
if (url != null){
ImageIcon icon = new ImageIcon(url);
if(icon != null){
fileSendbtn.setIcon(icon);
}
}
fileSendbtn.setPreferredSize(new Dimension(26,21));
fileSendbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (!(chatcontent.getChatframe().getToUno() ==null)){
JFileChooser fileahoose = new JFileChooser(".txt");
if(fileahoose.showDialog(chatcontent,"打开文件")== JFileChooser.APPROVE_OPTION){
File file = fileahoose.getSelectedFile();
FilePath.FileSendPath = file.getAbsolutePath();
String fileName = file.getName();
long fileLength = file.length();
FilePath.proLong = String.valueOf(file.length());
//打包
List filedata = new ArrayList();
HashMap filecontent = new HashMap();
filedata.add(fileName);
filedata.add(String.valueOf(fileLength));
filecontent.put("FILECONTENT",filedata);
sendfilePackage = new SrcPackage();
sendfilePackage.setData(filecontent);
sendfilePackage.setFrom(chatcontent.getChatframe().getUser().getUNO());
sendfilePackage.setTo(chatcontent.getChatframe().getToUno());
sendfilePackage.setTomeno(chatcontent.getChatframe().getUser().getUTERM());
sendfilePackage.setType(SrcPackage.FILESEND_TYPE);
//发送
try {
chatcontent.getChatframe().getOos().writeObject(sendfilePackage);
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
StyledDocument Content= chatcontent.getChatframe().getChatcontent().getJtpchatcontent().getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet(); // 样式
SimpleDateFormat tiem = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = tiem.format(new Date());
chatcontent.getJtpchatcontent().setEditable(true);
try {
Content.insertString(Content.getLength(), "您对 "+chatcontent.getChatframe().getToUser()+" 发送文件请求,正等待回复...或按" + "\n", style);
JButton btnCancle=new JButton("取消");
btnCancle.setBorder(null);
btnCancle.setSize(new Dimension(30,10));
chatcontent.getChatframe().getChatcontent().getJtpchatcontent().insertComponent(btnCancle);
Content.insertString(Content.getLength(), " 按钮取消文件传输\n\n", style);
Content.insertString(Content.getLength(), "\n\n", style);
int pos = chatcontent.getJtpchatcontent().getDocument().getLength();
chatcontent.getJtpchatcontent().scrollRectToVisible(new Rectangle(pos,pos));
chatcontent.getChatframe().getChatcontent().getJtpchatcontent().setEditable(false);
} catch (BadLocationException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
chatcontent.getJtpchatcontent().setEditable(false);
}
// }
}else{
JOptionPane.showMessageDialog(null, "请选择传文件的对象!", "提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
}
});
picSendbtn.setActionCommand("SENDPIC");
url = Images.class.getResource("faceIcon/send.gif");
if (url != null){
ImageIcon icon = new ImageIcon(url);
if(icon != null){
picSendbtn.setIcon(icon);
}
}
picSendbtn.setPreferredSize(new Dimension(26,21));
cbFont = new FontComboBox(1);
cbColor = new ColorComboBox();
try {
cbFontSize = new FontSizeComboBox(15, 55);
} catch (Exception e) {
e.printStackTrace();
}
if (cbFontSize!=null){
add(Box.createHorizontalStrut(2));
add(facebtn);
add(Box.createHorizontalStrut(4));
add(fileSendbtn);
add(Box.createHorizontalStrut(4));
add(picSendbtn);
add(Box.createHorizontalStrut(4));
add(cbFont);
add(Box.createHorizontalStrut(6));
add(cbColor);
add(Box.createHorizontalStrut(6));
add(cbFontSize);
add(Box.createHorizontalStrut(10));
add(Box.createHorizontalGlue());
}
cbColor.addItemListener(this);
cbFont.addItemListener(this);
cbFontSize.addItemListener(this);
}
/**
* 颜色,字体 ,字体大小 改变时触发的事件
*/
public void itemStateChanged(ItemEvent e) {
// TODO 自动生成方法存根
if (e.getSource() == cbColor){
jtpSend.setForeground(cbColor.getSelectedColors());
jtpSend2.setForeground(cbColor.getSelectedColors());
jtpSend.validate();
}else if (e.getSource() == cbFont){
Font font = new Font(cbFont.getSelectedFamily(),Font.BOLD,cbFontSize.getSelectedSize());
jtpSend.setFont(font);
jtpSend2.setFont(font);
}else if (e.getSource() == cbFontSize){
Font font = new Font(cbFont.getSelectedFamily(),Font.BOLD,cbFontSize.getSelectedSize());
jtpSend.setFont(font);
jtpSend2.setFont(font);
}
}
/**
* 获取颜色下拉控件
* @return
*/
public ColorComboBox getCbColor() {
return cbColor;
}
/**
* 获取字体下拉控件
* @return
*/
public FontComboBox getCbFont() {
return cbFont;
}
/**
* 获取字体大小下拉控件
* @return
*/
public FontSizeComboBox getCbFontSize() {
return cbFontSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -