本文通过JTextPanel来实现文档简易而不简单的文档编辑器,该文档编辑器的功能包括:
JTextPane docTextPane = new JTextPane();
final JComboBox fontModelCb = new JComboBox();// 字体样式下拉框,包括粗体,下划线和斜体
fontModelCb.setModel(new DefaultComboBoxModel(new String[] {
"\u7C97\u4F53", "\u4E0B\u5212\u7EBF", "\u659C\u4F53" }));
fontModelCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
System.out.println("fontModelCb itemStateChanged:"
+ sel.toString());
if (sel.equals("\u7C97\u4F53")) {
ac = new StyledEditorKit.BoldAction();
ac.putValue(Action.NAME, "Bold");
} else if (sel.equals("\u4E0B\u5212\u7EBF")) {
ac = new StyledEditorKit.UnderlineAction();
ac.putValue(Action.NAME, "Underline");
} else {
ac = new StyledEditorKit.ItalicAction();
ac.putValue(Action.NAME, "Italic");
}
fontModelCb.setAction(ac);
}
});
final JComboBox fontTypeCb = new JComboBox(); //设置字体下拉框,包括宋体和黑体
fontTypeCb.setModel(new DefaultComboBoxModel(new String[] {
"\u5B8B\u4F53", "\u9ED1\u4F53" }));
fontTypeCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
System.out.println("fontTypeCb itemStateChanged:"
+ sel.toString());
if (sel.equals("\u5B8B\u4F53")) {
ac = new StyledEditorKit.FontFamilyAction("宋体", "宋体");
} else {
ac = new StyledEditorKit.FontFamilyAction("黑体", "黑体");
}
fontTypeCb.setAction(ac);
}
});
final JComboBox fontSizeCb = new JComboBox();// 设置字体下拉框
fontSizeCb.setModel(new DefaultComboBoxModel(new String[] { "12", "14",
"18", "20", "30", "40" }));
fontSizeCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
fontSizeCb.setAction(new StyledEditorKit.FontSizeAction(sel
.toString(), Integer.parseInt(sel.toString())));
}
});
final JComboBox fontColorCb = new JComboBox(); //设置字体颜色下拉框
fontColorCb.setModel(new DefaultComboBoxModel(new String[] {
"\u9ED1\u8272", "\u7EA2\u8272", "\u84DD\u8272", "\u9EC4\u8272",
"\u7EFF\u8272" }));
fontColorCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
if (sel.equals("\u9ED1\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Black",
Color.black);
} else if (sel.equals("\u7EA2\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Red", Color.red);
} else if (sel.equals("\u84DD\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Green",
Color.green);
} else if (sel.equals("\u9EC4\u8272")) {
ac = new StyledEditorKit.ForegroundAction("Yellow",
Color.yellow);
} else {
ac = new StyledEditorKit.ForegroundAction("Blue",
Color.blue);
}
fontColorCb.setAction(ac);
}
});
final JComboBox fontBgColorCb = new JComboBox(); //设置字体背景下拉框
fontBgColorCb.setModel(new DefaultComboBoxModel(new String[] {
"\u65E0\u8272", "\u7070\u8272", "\u6DE1\u7EA2", "\u6DE1\u9EC4",
"\u6DE1\u84DD", "\u6DE1\u7EFF" }));
fontBgColorCb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object sel = e.getItem();
Action ac = null;
System.out.println("fontBgColorCb:" + sel.toString());
if (sel.equals("\u7070\u8272")) {// 灰色
ac = new DocBackgroundAction("LightBlack", new Color(200,
200, 200));
} else if (sel.equals("\u6DE1\u7EA2")) {// 淡红
ac = new DocBackgroundAction("LightRed", new Color(255,
200, 200));
} else if (sel.equals("\u6DE1\u9EC4")) { // 淡黄
ac = new DocBackgroundAction("LightGreen", new Color(255,
255, 200));
} else if (sel.equals("\u6DE1\u84DD")) {// 淡蓝
ac = new DocBackgroundAction("YLightYellow", new Color(200,
200, 255));
} else if (sel.equals("\u6DE1\u7EFF")) {// 淡绿
ac = new DocBackgroundAction("LightBlue", new Color(200,
255, 200));
}
if (ac != null) {
fontBgColorCb.setAction(ac);
}
}
});
JButton insertImageBt = new JButton("\u63D2\u5165\u56FE\u7247"); //插入图片按钮
insertImageBt.setAction(new DocImageAction("插入图片", docTextPane));
public class DocBackgroundAction extends StyledTextAction {
private static final long serialVersionUID = 1L;
public DocBackgroundAction(String nm, Color bg) {
super(nm);
this.bg = bg;
}
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
Color fg = this.bg;
if ((e != null) && (e.getSource() == editor)) {
String s = e.getActionCommand();
try {
fg = Color.decode(s);
} catch (NumberFormatException nfe) {
}
}
if (fg != null) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, fg);
setCharacterAttributes(editor, attr, false);
} else {
UIManager.getLookAndFeel().provideErrorFeedback(editor);
}
}
}
private Color bg;
}
public class DocImageAction extends StyledTextAction {
private static final long serialVersionUID = 1L;
public DocImageAction(String nm, JTextPane panl) {
super(nm);
this.panl = panl;
}
public void actionPerformed(ActionEvent e) {
JFileChooser f = new JFileChooser(); // 查找文件
f.showOpenDialog(null);
System.out.println(f.getSelectedFile());
ImageIcon icon = createImageIcon(f.getSelectedFile(), "a cute pig");
JTextPane editor = this.panl;
if (editor != null) {
System.out.println("I am in here");
StyledDocument doc = getStyledDocument(editor);
editor.setCaretPosition(doc.getLength()); // 设置插入位置
editor.insertIcon(icon); // 插入图片
}
}
private JTextPane panl;
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = DocImageAction.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}