为什么 UndoManager.canRedo() return false 即使 redo() 工作正常?

Why would UndoManager.canRedo() return false even though redo() works fine?

我使用 Java 的撤消包中的某些方法得到了相互矛盾的结果。在我的程序中,我在 UndoManager 实例上调用 canRedo(),returns 为 false。这会让我相信我无法在那一刻重做存储在 UndoManager 中的任何操作。然而,当我尝试时,最后撤消的操作被正确地重做并且没有抛出 CannotRedoException 。对我来说,这种行为似乎很矛盾,我不确定是什么原因造成的。

下面的代码是一个单独的、单线程的草稿文件,专门为这个问题创建。

import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;

class UndoManagerRedoScratch {
    public static void main(String[] args) {
        UndoManager actionList = new UndoManager();
        actionList.addEdit(new SomeUndoableEdit());

        /* See whether SomeUndoableEdit is undoable. */
        try {
            System.out.println("Action can be undone: " + actionList.canUndo());
            actionList.undo();
        } catch (Exception e) {
            System.out.println("Undo failed");
        }

        /* See whether SomeUndoableEdit is redoable. */
        try {
            System.out.println("Action can be redone: " + actionList.canRedo());
            actionList.redo();
        } catch (Exception e) {
            System.out.println("Redo failed");
        }
    }
}

class SomeUndoableEdit extends AbstractUndoableEdit {

    public SomeUndoableEdit() {
        System.out.println("SomeUndoableEdit has been created");
    }

    @Override
    public void undo() throws CannotUndoException {
        System.out.println("SomeUndoableEdit has been undone.");
    }

    @Override
    public void redo() throws CannotRedoException {
        System.out.println("SomeUndoableEdit has been redone.");
    }
}

输出:

SomeUndoableEdit has been created
Action can be undone: true
SomeUndoableEdit has been undone.
Action can be redone: false
SomeUndoableEdit has been redone.

如您所见,redo() 成功执行,没有抛出 CannotRedoException,但 canUndo() 返回 false。同样,这对我来说似乎是矛盾的。

有什么想法吗?

根据 jre 中的某些实现,例如 javax.swing.undo.StateEdit,您应该调用 super.undo()super.redo() 作为覆盖方法中的第一个调用。

所以你的情况:

class SomeUndoableEdit extends AbstractUndoableEdit {

    public SomeUndoableEdit() {
        System.out.println("SomeUndoableEdit has been created");
    }

    @Override
    public void undo() throws CannotUndoException {
        super.undo();
        System.out.println("SomeUndoableEdit has been undone.");
    }

    @Override
    public void redo() throws CannotRedoException {
        super.redo();
        System.out.println("SomeUndoableEdit has been redone.");
    }
}