如何在java中为动作实现简单的撤消/重做? [英] How do I implement a simple undo/redo for actions in java?
问题描述
我已经创建了一个XML编辑器,我在最后阶段陷入困境:添加撤消/重做功能。
I've created an XML editor and I'm stuck at the last phase: adding undo/redo functionality.
我只需添加撤消/当用户向JTree添加元素,属性或文本时重做。
I've only got to add undo/redo for when users add elements, attributes, or text to the JTree.
我仍然很新,但今天在学校我试图(不成功)创建两个stack object []调用undo和redo并添加执行的操作。
I'm still quite new at this but in school today I attempted (unsuccessfully) to create two stack object []'s called undo and redo and to add the actions performed into them.
对于实例,我有:
Action AddElement() {
// some code
public void actionPerformed(ActionEvent e) {
performElementAction();
}
}
performElementAction实际上只是向JTree添加了一个元素。
the performElementAction just actually adds an Element to the JTree.
我想添加一种方法来将这个动作添加到我的撤销堆栈中。是否有一种简单的方法来实现undo.push(执行整个动作)或什么?
I want to add a way to add this action performed to my undo stack. is there a simple way to just undo.push( the entire action performed) or something?
抱歉听起来像个坏人,但这就是我:(
Sorry for sounding like a baddie, but that is what I am :(
推荐答案
查看命令模式,其用途包括实现撤消/重做功能。
Take a look at the Command Pattern, its uses include implementing undo/redo functionality.
这篇关于如何在java中为动作实现简单的撤消/重做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!