可以采用momento设计模式,具体我也没有做过!

解决方案 »

  1.   

    如果是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
      

  2.   

    如果是自己写的class , 就要自己实现undo 了,momento pattern 是一种方式, 或 Command pattern. 在java 中 , 看一下 javax.undo.* package. 还是很容易理解的,只是好的实现 和整个系统的构架很有关系。