- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当在 Swing 中构建 GUI 时,我总是遇到一个概念问题,以至于我以一种糟糕的方式结束了 hacking。我想知道这方面的良好做法。
让我们举一个简单的例子。
我有一个 JComponent,我想在其上实现撤消/重做操作,以及一些添加/删除其他组件的操作。所以,我会开始写:
public class MyComponent extends JComponent {
private UndoManager undo = new UndoManager();
public void addComponent(JComponent comp) {
this.add(comp);//Add the component to the view
undo.addEdit(new ComponentAddedEdit(comp));//make this action undoable
fireComponentAdded(comp);//warn the listeners that the action occured
}
}
然后开始问题。在我的 ComponentAddedEdit
中,我会想到类似的东西:
public class ComponentAddedEdit extends AbstractUndoableEdit {
private final JComponent comp;
public ComponentAddedEdit(JComponent comp) {this.comp = comp;}
@Override
public void undo() throws CannotUndoException {
MyComponent.this.removeComponent(comp);
}
@Override
public void redo() throws CannotRedoException {
MyComponent.this.addComponent(comp);
}
}
当然,它不起作用,因为重做操作将为 UndoManager 创建一个新的编辑。所以我需要创建一个像这样的新方法:
public void addComponentNoUndo() {
this.add(comp);//Add the component to the view
fireComponentAdded(comp);//warn the listeners that the action occured
}
最后,对于“add”操作,我最终得到了 3 个名称相似的方法:add
、addComponent
和 addComponentNoUndo
。如果我有更多的 Action ,更复杂的 Action ,它会变得非常困惑。那么你会怎么做呢?
最佳答案
我不太喜欢 swing,所以我可能不会给你具体的实现答案,但请参阅此 tutorial on memento pattern .
您肯定需要根据您的需求进行定制,但我认为这是正确的方法。
尝试编写自己的“状态”类,而不是简单地使用字符串。
如果您想撤消某些操作,请加载旧状态。如果您不希望用户能够撤消某些操作,只需不加载之前的状态即可,告诉用户这是不可能的。考虑添加一个 boolean 标志“undoable”到你的“state”类。
编辑
类似的东西
class state extends JComponent {
private boolean undoable;
private JComponent toSave;
public state (boolean undoable, JComponent toSave, ...) {
this.undoable = undoable;
this.toSave = toSave;
...
}
...
}
关于java - JComponent 上有 UndoManager 和监听器时的 OO 概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34333192/
HTML5 规范提到了一个 UndoManager,它可用于查询用户编辑过的文档的先前状态 (http://www.whatwg.org/specs/web-apps/current-work/#un
我使用 UndoManager在我的 Swing 应用程序中。如果在 UndoManager 上调用了 undo()、redo()、addEdit() 或其他方法,Undo 和 Redo 按钮最终必须
我有 3 个 MOC。 MainThread MOC 显示内容(使用 undomanager) 后台保存 MOC 以将数据保存到光盘(连接到存储) Backgorund-update MOC 从服务器
我正在使用 UndoManager 来捕获我的 JTextArea 中的更改。 setText() 方法会删除所有内容,然后粘贴文本。当我撤消时,我首先看到一个空白区域,然后它会显示它之前的文本。 如
我对 ACE 很陌生编辑器和 javascript 一般情况下,除了以下内容之外,我已经设法实现了大部分我想要的: 我想启用或禁用“保存”按钮,具体取决于是否对文档进行了未完成的修改,并且我已尝试通过
我希望能够从我的文档模型中访问 UndoManager,这样我就可以在模型中注册撤消操作: // Assume I've extended MyDocument to conform to Refer
我有一个名为 appointment 的 NSManagedObject,我编辑了它的属性。如果我的用户按下取消,我想撤消所有这些编辑。 如果我这样做(示例代码) [[appointment mana
使用 ACE Editor , 它看起来好像使用 editor.getSession().getUndoManager() 和一个已经用 editor.setSession() 设置的 session
我有一个 JTextArea 和一个 JComboBox 允许我循环浏览各种打开的文件 - JTextArea 的内容随着我的选择而改变一个不同的文件。我试图为每个文件维护一个不同的撤消缓冲区,并为每
我正在尝试在我的模型中实现撤消/重做。所以我让我的模型类成为 NSResponder 的子类,然后实现了以下内容: 注意:此代码是根据评论后的更多研究编辑的 func setAnnotations(_
我正在努力向使用 undoManager 的 Swift 库添加一些测试。在库的演示中,将以下打印添加到 viewDidAppear: 会导致打印 NSUndoManagerObject。 overr
我对如何在 Firefox 中使用 undoManager 有点困惑。根据Mozilla , Firefox 20 现在支持它,但我不知道在哪里。我已经在 document、Objects 等下进行了
我想在我的应用程序中应用撤消和重做操作。我阅读了苹果文档并用谷歌搜索了它,但没有得到正确的解决方案。在我的应用程序中,我在 UIImage 上应用了很多图像操作,我想存储每个状态并相应地应用 Undo
我将以下代码用于核心数据单例。下面是我的代码。 (基于 NachoMan 的 blog 。但是代码来自他的要点。 // DataManager.h #import #import extern N
如何在 Swift 中使用 UndoManager(以前的 NSUndoManager)? 这是我尝试复制的 Objective-C 示例: [[undoManager prepareWithInvo
我正在尝试在类似绘画的程序中设置UndoManager,但不幸失败了。我一直在查看的示例程序是文本编辑器 ( Example ),它们调用 JTextComponent 类的 addUndoableE
我尝试使用 undomanager 撤消/重做编辑顶点。 图形对象经过测试。但我不知道该怎么做编辑顶点撤消/重做。 顶点是否可以撤消/重做? 我找了很多例子都没有找到答案。 我是韩国初学者程序员。帮帮
使用 Swift 3.02 (Xcode 8.2) 我引用了答案中描述的撤消解决方案:https://stackoverflow.com/a/31450475/7448394 . 在下面的代码中,我使
我只是试图让用户在我的应用程序中执行的操作撤消。默认情况下,用户所做的任何文本编辑都具有撤消的好处,但以其他方式(通过我的代码)完成的任何操作都没有。 我可以看到文档解释说我需要获取 NSUndoMa
为什么@Environment UndoManager不更新其 canUndo当它的堆栈中有 Action 时的属性?我有一个 View ,它有一个可以使用取消/重做功能的 child ,但由于某种原
我是一名优秀的程序员,十分优秀!