1

我正在尝试设置UndoManager一个类似绘画的程序,但不幸地失败了。我一直在看的示例程序是文本编辑器(Example),它们调用addUndoableEditListenerclass的方法JTextComponent

我应该如何设置 UndoManager 以使用画布?

public class Pisi extends JFrame implements MouseMotionListener, MouseListener,
    UndoableEditListener {
ArrayList<ArrayList<Point>> store = new ArrayList<ArrayList<Point>>();
ArrayList<Point> pts = new ArrayList<Point>();
ArrayList<Point> newRed;
ArrayList<Point> currentRed = new ArrayList<Point>();
JPanel panel;
Point start;
static int xsize = 500;
static int ysize = 350;
int listNumber = 0;
int lastPointed = -1;
int pointed = -1;
int clicked = -1;
UndoManager undoManager = new UndoManager();
UndoAction undoAction = new UndoAction();
RedoAction redoAction = new RedoAction();
protected MyUndoableEditListener l = new MyUndoableEditListener();


public Pisi() {
    panel = new JPanel() {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
        }
    };
    setSize(xsize, ysize);
    setResizable(false);
    getContentPane().setLayout(null);
    getContentPane().add(panel);
    setLocationRelativeTo(null);
    setVisible(true);
    panel.setLocation(0, -11);
    this.addMouseMotionListener(this);
    this.addMouseListener(this);
    **this.addUndoableEditListener(this);**
}

public static void main(String[] args) {
    Pisi d = new Pisi();
}

*... more code...*
}

所有输入将不胜感激。

4

1 回答 1

2

您需要为所有应该可撤消/可重做的用户操作创建编辑类。这些类必须实现UndoableEdit(最好通过继承AbstractUndoableEdit)。然后,您可以将这些编辑类与UndoManagerUndoableEditSupport的实例一起使用。

您可以将 UndoableEdit 对象直接添加到 UndoManager(它有一个 addEdit 方法)。如果你想管理 UndoableEditListener 对象(例如通知菜单项或按钮),你可以使用 UndoableEditSupport - 它有你正在寻找的 addUndoableEditListener。

于 2013-01-15T22:25:51.913 回答