【问题标题】:Java 6: How to set up UndoManager to work with a canvas?Java 6:如何设置 UndoManager 以使用画布?
【发布时间】:2012-12-30 02:52:39
【问题描述】:

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

我应该如何设置 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...*
}

所有输入将不胜感激。

【问题讨论】:

  • 你能设置它与面板一起工作吗?
  • 看来你对监听器、面板、画布的看法不对。抱歉,我不明白你在说什么,因为它与问题相矛盾

标签: java swing canvas jpanel undo


【解决方案1】:

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

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

【讨论】:

  • @Ibalazscs,你愿意提供一个此类的例子吗?
  • 好吧,一旦我写了一个支持无限撤消的开源图像编辑器,你可以从这里下载源代码:pixelitor.sourceforge.net(此代码在 pixelitor.history 包中)
  • @Ibalazscs,它是单独下载的吗?我找不到具有此名称的下载。
  • 查看所有下载/1.1.2 文件夹/pixelitor_1.1.2_src.zip
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 2012-03-04
  • 2014-07-14
相关资源
最近更新 更多