📄 mailedit.java
字号:
package EmailManage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import javax.mail.MessagingException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.undo.UndoManager;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
//new ImageIcon(file.getAbsoluteFile().toString());
public class MailEdit implements ActionListener//extends JFrame
{
private static JComboBox cbotexthtml;
private JComboBox cboHtmlPic;
private JComboBox cboAtt;
private static JComboBox cboPriority;
private JTextField TxtSubject;
private JTextField TxtBcc;
private JTextField TxtCC;
private JTextField TxtTo;
private static JComboBox cboFontSize;
private static JComboBox cboFont;
private JScrollPane scrollPane ;
private static JTextPane editorPane;
public String dateformat = "yyyy-MM-dd HH:mm";
static String[] fonesize = { "8", "9", "10", "11", "12", "14", "16", "18",
"20", "22", "24", "26", "28", "36", "48", "72" };
static String[] fontlist = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
new MailEdit();
InitCom();
editorPane.setEditable(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void InitCom(){
//new UndoWrapper(editorPane);
UndoManager undoManager = new UndoManager();
editorPane.getDocument().addUndoableEditListener(undoManager);
// editorPane.setFocusTraversalPolicyProvider(true);
editorPane.setSelectedTextColor(Color.BLUE);
// editorPane.setRequestFocusEnabled(true);
// editorPane.setInheritsPopupMenu(true);
// editorPane.setEditorKit(HTMLEditorKit);
// editorPane.setDocument(doc);
editorPane.setDoubleBuffered(true);
editorPane.setSelectionColor(Color.RED);
editorPane.setFont(new Font("宋体", Font.PLAIN, 14));
editorPane.setDragEnabled(true);
editorPane.setContentType("text/html");
editorPane.setDisabledTextColor(Color.GRAY);
editorPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, 12);
setCharacterAttributes(editorPane, attr, false);
if (fontlist.length > 0) {
for (int i = 0; i < fontlist.length; i++) {
cboFont.addItem(fontlist[i]);
}
}
if (fonesize.length > 0) {
for (int i = 0; i < fonesize.length; i++) {
cboFontSize.addItem(fonesize[i]);
}
}
cboPriority.addItem("高优先级");
cboPriority.addItem("普通优先级");
cboPriority.addItem("低优先级");
cboPriority.setSelectedIndex(1);
cbotexthtml.addItem("文本");
cbotexthtml.addItem("HTML");
cbotexthtml.setSelectedIndex(1);
}
/**
* Create the frame
*/
public MailEdit() {
super();
JFrame frame = new JFrame("新邮件");
// setIconImage(new ImageIcon(".\\icons\\mail.gif"));
// setBounds(100, 100, 500, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(659, 461);
frame.getContentPane().setLayout(
new FormLayout(
new ColumnSpec[] {
new ColumnSpec("right:77px"),
new ColumnSpec("left:1dlu"),
new ColumnSpec("276dlu:grow(1.0)"),
new ColumnSpec("2dlu")},
new RowSpec[] {
new RowSpec("top:35px"),
new RowSpec("top:3dlu"),
new RowSpec("16dlu"),
new RowSpec("top:2dlu"),
new RowSpec("16dlu"),
new RowSpec("top:2dlu"),
new RowSpec("16dlu"),
new RowSpec("top:2dlu"),
new RowSpec("16dlu"),
new RowSpec("top:2dlu"),
new RowSpec("19dlu"),
new RowSpec("top:0dlu"),
new RowSpec("bottom:111dlu:grow(1.0)"),
FormFactory.RELATED_GAP_ROWSPEC,
new RowSpec("bottom:19dlu"),
new RowSpec("top:2dlu"),
FormFactory.DEFAULT_ROWSPEC}));
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scrollPane.setBackground(Color.RED);
scrollPane.setAutoscrolls(true);
frame.getContentPane().add(scrollPane,
new CellConstraints("1, 13, 4, 1, fill, fill"));
editorPane = new JTextPane();
scrollPane.setViewportView(editorPane);
// 对齐方式工具栏
final JToolBar toolBar = new JToolBar();
frame.getContentPane().add(toolBar,
new CellConstraints(1, 11, 4, 1, CellConstraints.LEFT,
CellConstraints.DEFAULT));
// 导入html文件
final JButton btnLoadHtml = new JButton(new ImageIcon(".\\icons\\open.gif"));
btnLoadHtml.setActionCommand("LoadHtml");
toolBar.add(btnLoadHtml);
btnLoadHtml.setToolTipText("导入HTML");
btnLoadHtml.addActionListener(this);
// 保存HTML
final JButton btnSaveHtml = new JButton(new ImageIcon(".\\icons\\save.gif"));
toolBar.add(btnSaveHtml);
btnSaveHtml.setActionCommand("SaveHtml");
btnSaveHtml.addActionListener(this);
final JSeparator separator = new JSeparator();
separator.setMinimumSize(new Dimension(4, 0));
separator.setMaximumSize(new Dimension(4, 0));
toolBar.add(separator);
final JButton BtnAlignLeft = new JButton(new ImageIcon(".\\icons\\justifyleft.gif"));
BtnAlignLeft.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, 0);
setParagraphAttributes(editorPane, attr, false);
}
});
toolBar.add(BtnAlignLeft);
final JButton BtnAlignMid = new JButton(new ImageIcon(".\\icons\\justifycenter.gif"));
BtnAlignMid.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, 1);
setParagraphAttributes(editorPane, attr, false);
}
});
toolBar.add(BtnAlignMid);
final JButton BtnAlignRight = new JButton(new ImageIcon(".\\icons\\justifyright.gif"));
BtnAlignRight.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, 2);
setParagraphAttributes(editorPane, attr, false);
}
});
toolBar.add(BtnAlignRight);
toolBar.addSeparator();
final JButton BtnLeftIden = new JButton(new ImageIcon(".\\icons\\outdent.gif"));
toolBar.add(BtnLeftIden);
final JButton BtnRightIden = new JButton(new ImageIcon(".\\icons\\indent.gif"));
toolBar.add(BtnRightIden);
//orderlist
final JButton btnOrderList = new JButton(new ImageIcon(".\\icons\\orderedlist.gif"));
btnOrderList.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
try {
insertHTML(editorPane,"<OL> <LI></LI></OL>");//, editorPane.getSelectionStart());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
// btnOrderList.setText("O");
toolBar.add(btnOrderList);
final JButton BtnUnOderList = new JButton(new ImageIcon(".\\icons\\unorderedlist.gif"));
// BtnUnOderList.setText("UO");
toolBar.add(BtnUnOderList);
// BtnUnOderList.disable();
final JToolBar toolBarColor = new JToolBar();
toolBar.add(toolBarColor);
// 字体颜色
final JButton BtnForeColor = new JButton(new ImageIcon(".\\icons\\forecolor.gif"));
BtnForeColor.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
Color color = Color.black;
color = JColorChooser
.showDialog(scrollPane, "Choose a color", color);
if (color == null) {
color = Color.black;
} else {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, color);
setCharacterAttributes(editorPane, attr, false);
}
}
});
toolBarColor.add(BtnForeColor);
// BtnForeColor.setText("Btn");
// 背景颜色
final JButton BtnBackColor = new JButton(new ImageIcon(".\\icons\\bgcolor.gif"));
BtnBackColor.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
Color color = Color.black;
color = JColorChooser
.showDialog(scrollPane, "Choose a color", color);
if (color == null) {
color = Color.black;
} else {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, color);
setCharacterAttributes(editorPane, attr, false);
}
}
});
// BtnBackColor.setText("Btn");
toolBarColor.add(BtnBackColor);
toolBarColor.addSeparator();
//插入横线
final JButton btnInsertLine = new JButton(new ImageIcon(".\\icons\\line.gif"));
btnInsertLine.setToolTipText("插入横线");
toolBarColor.add(btnInsertLine);
btnInsertLine.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
try {
insertHTML(editorPane,"<p></p><HR>");//, editorPane.getSelectionStart());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
// 插入图片
final JButton btnInsertImage = new JButton(new ImageIcon(".\\icons\\image.gif"));
btnInsertImage.setToolTipText("插入图片");
btnInsertImage.setActionCommand("InsertImage");
btnInsertImage.addActionListener(this);
toolBarColor.add(btnInsertImage);
// btnButton_1.setText("Btn");
// 字体样式工具栏
final JToolBar toolBatFontStyle = new JToolBar();
toolBar.add(toolBatFontStyle);
// 粗体
final JButton BtnFontB = new JButton(new ImageIcon(".\\icons\\bold.gif"));
BtnFontB.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
StyledEditorKit kit = getStyledEditorKit(editorPane);
MutableAttributeSet attr = kit.getInputAttributes();
boolean bold = (StyleConstants.isBold(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, bold);
setCharacterAttributes(editorPane, sas, false);
}
});
// BtnFontB.setText("");
toolBatFontStyle.add(BtnFontB);
// 下划线
final JButton BtnFontU = new JButton(new ImageIcon(".\\icons\\underline.gif"));
BtnFontU.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
StyledEditorKit kit = getStyledEditorKit(editorPane);
MutableAttributeSet attr = kit.getInputAttributes();
boolean underline = (StyleConstants.isUnderline(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setUnderline(sas, underline);
setCharacterAttributes(editorPane, sas, false);
}
});
// BtnFontU.setText("Btn");
toolBatFontStyle.add(BtnFontU);
toolBatFontStyle.addSeparator();
// 斜体
final JButton BtnFontI = new JButton(new ImageIcon(".\\icons\\italic.gif"));
BtnFontI.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent e) {
StyledEditorKit kit = getStyledEditorKit(editorPane);
MutableAttributeSet attr = kit.getInputAttributes();
boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setItalic(sas, italic);
setCharacterAttributes(editorPane, sas, false);
}
});
toolBatFontStyle.add(BtnFontI);
// 字体及大小工具栏
final JToolBar toolBarFontNameSize = new JToolBar();
toolBar.add(toolBarFontNameSize);
/** * 字体*/
cboFont = new JComboBox();
cboFont.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent arg0) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontFamily(attr, cboFont.getSelectedItem()
.toString());
setCharacterAttributes(editorPane, attr, false);
}
});
toolBarFontNameSize.add(cboFont);
// 字体大小
cboFontSize = new JComboBox();
cboFontSize.setMaximumRowCount(12);
cboFontSize.setSelectedItem("12");
cboFontSize.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent arg0) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, Integer.parseInt(cboFontSize
.getSelectedItem().toString()));
setCharacterAttributes(editorPane, attr, false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -