使用HTML的文档格式:
HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument document = (HTMLDocument) editorKit.createDefaultDocument();
textPane = new JTextPane(document);
textPane.setContentType("text/html");
JEditorPane已经提供了字体,颜色,字号,加粗等基本操作的Action
Action[] actions = TextAction.augmentList(editorPane.getActions(), defaultActions);
commands = new Hashtable();
for(int i = 0; i < actions.length; i++)
{
Action a = actions[i];
commands.put(a.getValue(Action.NAME), a);
}
//函数,返回Action
protected Action getAction(String cmd)
{
return (Action) commands.get(cmd);
}
Action boldAction = this.getAction("font-bold");
Action copyAction = this.getAction("copy-to-clipboard");
Action cutAction = this.getAction("cut-to-clipboard");
Action pasteAction = this.getAction("paste-from-clipboard");
Action boldAction = this.getAction("font-bold");
Action italicAction = this.getAction("font-italic");
Action underlineAction = this.getAction("font-underline");
Action leftAction = this.getAction("left-justify");
Action centerAction = this.getAction("center-justify");
Action rightAction = this.getAction("right-justify");
//颜色:
ActionListener colorActionListener = new StyledEditorKit.ForegroundAction("set-foreground-", color);
colorActionListener.actionPerformed(new ActionEvent(editorPane, 0, ""));
//行间距:
if(editorPane instanceof JTextPane)
{
StyledDocument doc = ((JTextPane) editorPane).getStyledDocument();
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setLineSpacing(attr, (float) DoubleUtil.getAsDouble(t));
doc.setParagraphAttributes(editorPane.getSelectionStart(), editorPane.getSelectionEnd(), attr, false);
}
//插入图片(针对JTextPane):
((JTextPane)editorPane).insertIcon(new ImageIcon(file.getPath()));
//保存(随机一个文件名):
BufferedInputStream d = (BufferedInputStream) this.getPage().getContent();
byte[] c = new byte[d.available()];
d.read(c);
HTMLDocument doc = (HTMLDocument) this.getDocument();
Random rand = new Random();
int t = rand.nextInt();
File f = new File("m" + t + ".html");
FileOutputStream fstrm = new FileOutputStream(f);
ObjectOutput ostrm = new ObjectOutputStream(fstrm);
ostrm.writeObject(this.getDocument());
ostrm.flush();
jtextpanel
FileOutputStream fstrm = new FileOutputStream(file);
ObjectOutput ostrm = new ObjectOutputStream(fstrm);
ostrm.writeObject(this.getDocument());
Log.info(this.getDocument().getText(0, this.getDocument().getLength()));
ostrm.flush();
//打开JEditorPane
this.setPage("file:///" + f.getAbsolutePath());
JTextPanel
File f = new File("temptextpanelfile");
FileInputStream fin = new FileInputStream(f);
ObjectInputStream istrm = new ObjectInputStream(fin);
Document doc = (Document) istrm.readObject();
if(this.getDocument() != null)
this.getDocument().removeUndoableEditListener(toolbar.undoHandler);
this.setDocument(doc);
doc.addUndoableEditListener(toolbar.undoHandler);
resetUndoManager();
validate();
istrm.close();
fin.close();
f.delete();
一般来说使用JEditorPane时使用setPage(url)来显示,储存时存为文件。
在我的应用中,我不想存为文件,我想得到完整的HTML格式的字符。所以我使用getText()(不要用document来返回)来返回字符,用setText来显示,基本上差不多,但有个问题就是用getText返回时会自动去掉你输入的空格。搞死人,Google上猛搜索了一下后得到如下解决方法:
//新建一个DocumentFilter
public class CustDocumentFilter extends DocumentFilter
{
static String space = " ";
static String spaceSeq = "\240";
static String tab = "\t";
static String tabSeq = "\240\240\240\240";
// important: always overwrite all three methods
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException
{
string = doFiltering(string);
fb.insertString(offset, string, attr);
}
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException
{
fb.remove(offset, length);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException
{
text = doFiltering(text);
fb.replace(offset, length, text, attrs);
}
private String doFiltering(String in)
{
String out = in.replaceAll(space, spaceSeq);
return out.replaceAll(tab, tabSeq);
}
}
然后使用
((HTMLDocument)this.getDocument()).setDocumentFilter(new CustDocumentFilter());
这样就可以避免你输入的空格别吃掉了。
下边是一个JTextPanel的基本应用: