在Java中,Package: javax.swing.undo是专门用来 撤消与重做的包.
简单使用的基本步骤如下:
1) 实现UndoableEditListener接口中的方法
public void undoableEditHappened(UndoableEditEvent e) {
Model.addEdit(e.getEdit()); 基中Model为UndoManager的实例.
}
2) 在对应的编辑器的Document中注册如下:
mEditPanel.getDocument().addUndoableEditListener((UndoableEditListener)pDocumentListener);
其中pDocumentListener为实现了UndoableEditListener接口类的实例.
3) 给对应的按钮编写事件,如下:
else if(strCommand.equals("Redo")){
try{
um.redo();
}catch(Exception er){}
}
else if(strCommand.equals("Undo")){
try{
um.undo();
}catch(Exception er){}
} //注意如果不能撤消或者不能重做时,throw CannotUndoException .
这里只是Undo包的初级使用,如果要使用这个包的高级功能,还得撑握包中一些类的使用.