如果是swing 的 JText的undo 的话 ,以下是一个考来的例子,java.sun.com有很多例子JTextArea textarea = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textarea.getDocument(); //The next two lines should be in one line. doc.addUndoableEditListener( new UndoableEditListener() { //The next two lines should be in one line. public void undoableEditHappened( UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); textarea.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textarea.getInputMap().put(KeyStroke.getKeyStroke( "control Z"), "Undo"); textarea.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); http://developer.java.sun.com/developer/codesamples/javax.swing.undo/236.html (1 of 2) [8/1/2000 7:51:31 AM] } } catch (CannotRedoException e) { } } }); textarea.getInputMap().put(KeyStroke.getKeyStroke( "control Y"), "Redo"); //Code Samples from the Java Developers Almanac 2000
final UndoManager undo = new UndoManager();
Document doc = textarea.getDocument();
//The next two lines should be in one line.
doc.addUndoableEditListener(
new UndoableEditListener() {
//The next two lines should be in one line.
public void undoableEditHappened(
UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
});
textarea.getActionMap().put("Undo",
new AbstractAction("Undo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
}
}
});
textarea.getInputMap().put(KeyStroke.getKeyStroke(
"control Z"), "Undo");
textarea.getActionMap().put("Redo",
new AbstractAction("Redo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canRedo()) {
undo.redo();
http://developer.java.sun.com/developer/codesamples/javax.swing.undo/236.html (1 of 2) [8/1/2000 7:51:31 AM]
}
} catch (CannotRedoException e) {
}
}
});
textarea.getInputMap().put(KeyStroke.getKeyStroke(
"control Y"), "Redo");
//Code Samples from the Java Developers Almanac 2000