做一个类似于word的编辑器。用JEditorPane作为文本编辑区。根据查到的资料是这样来实现redo和undo。
HTMLEditorKit kit = new HTMLEditorKit();
JEditorPane.registerEditorKitForContentType("text/html",
"HTMLEditorKit");
editorPane.setContentType("text/html");
editorPane.setEditorKit(kit);
editorPane.setEditable(true);
UndoManager undo = new UndoManager();
editorPane.getDocument().addUndoableEditListener(new
UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
System.out.println("undo redo performed");
undo.addEdit(e.getEdit()); // undoAction.updateUndoState();
// redoAction.updateRedoState();
if(!editPanelButtonBack.isEnabled())
editPanelButtonFoward.setEnabled(false);
}
});
在添加了这段相应后,在编辑区里进行输入,删除等操作都不会执行“undo.addEdit(e.getEdit())”这句代码,所以undo就无法执行。难道是这些操作都不可以undo?如果这样,undo和redo就没有什么作用了。
HTMLEditorKit kit = new HTMLEditorKit();
JEditorPane.registerEditorKitForContentType("text/html",
"HTMLEditorKit");
editorPane.setContentType("text/html");
editorPane.setEditorKit(kit);
editorPane.setEditable(true);
UndoManager undo = new UndoManager();
editorPane.getDocument().addUndoableEditListener(new
UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
System.out.println("undo redo performed");
undo.addEdit(e.getEdit()); // undoAction.updateUndoState();
// redoAction.updateRedoState();
if(!editPanelButtonBack.isEnabled())
editPanelButtonFoward.setEnabled(false);
}
});
在添加了这段相应后,在编辑区里进行输入,删除等操作都不会执行“undo.addEdit(e.getEdit())”这句代码,所以undo就无法执行。难道是这些操作都不可以undo?如果这样,undo和redo就没有什么作用了。
http://blog.csdn.net/mq612/archive/2006/09/16/1231605.aspx