首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(撤消/恢复|| KeyStrokes)在JEditorPane中不起作用

(撤消/恢复|| KeyStrokes)在JEditorPane中不起作用
EN

Stack Overflow用户
提问于 2016-09-21 17:30:37
回答 1查看 316关注 0票数 0

我希望有人能帮助我,因为我已经为这个问题斗争了太久,我的大脑无法理解它。

我正在尝试在JEditorPane扩展类中实现Undo/Redo (我在http://alvinalexander.com/java/java-undo-redo找到的):

代码语言:javascript
复制
public class TextEditor extends JEditorPane {

class UndoHandler implements UndoableEditListener {

  @Override
  public void undoableEditHappened(UndoableEditEvent e) {
    undoManager.addEdit(e.getEdit());
    undoAction.update();
    redoAction.update();
  }
}

class UndoAction extends AbstractAction {
  public UndoAction() {
    super("Undo");
    setEnabled(false);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    System.out.println("UNDO!");
    try {
      undoManager.undo();
    } catch (CannotUndoException ex) {
      // TODO deal with this
      ex.printStackTrace();
    }
    update();
    redoAction.update();
  }

  protected void update() {
    if (undoManager.canUndo()) {
      setEnabled(true);
      putValue(Action.NAME, undoManager.getUndoPresentationName());
    } else {
      setEnabled(false);
      putValue(Action.NAME, "Undo");
    }
  }
}

class RedoAction extends AbstractAction {
  public RedoAction() {
    super("Redo");
    setEnabled(false);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
      System.out.println("REDO!");
    try {
      undoManager.redo();
    } catch (CannotRedoException ex) {
      ex.printStackTrace();
    }
    update();
    undoAction.update();
  }

  protected void update() {
    if (undoManager.canRedo()) {
      setEnabled(true);
      putValue(Action.NAME, undoManager.getRedoPresentationName());
    } else {
      setEnabled(false);
      putValue(Action.NAME, "Redo");
    }
  }
}

private UndoHandler undoHandler = new UndoHandler();
private UndoManager undoManager = new UndoManager();
private UndoAction undoAction = new UndoAction();
private RedoAction redoAction = new RedoAction();

public TextEditor() {
    super();
    this.setEditorKit(new ShowSpecCharsEditorKit());

    this.getDocument().addUndoableEditListener(undoHandler);
    KeyStroke undoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
    KeyStroke redoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);

    this.getInputMap().put(undoKeystroke, "undoKeystroke");
    this.getActionMap().put("undoKeystroke", undoAction);

    this.getInputMap().put(redoKeystroke, "redoKeystroke");
    this.getActionMap().put("redoKeystroke", redoAction);


    this.addCaretListener(new CaretListener() {

        @Override
        public void caretUpdate(CaretEvent e) {

            ((EditorTab)getParent().getParent()).updateTabTitle(true);
        }
    });
}

@Override
public void read(Reader r, Object desc) throws IOException{
    super.read(r, desc);
}

}

由于某些原因,我的按键没有被触发,或者撤销/重做就是不起作用。

我不能让它工作。有人能给我指出点什么吗?

EN

回答 1

Stack Overflow用户

发布于 2016-09-21 17:54:54

当我评论的时候,你的代码看起来运行得很好

来自您的代码的this.setEditorKit(new ShowSpecCharsEditorKit());

这可能是编辑器工具包的问题,请检查keyStrokes和Actions上的自定义EditorKit (ShowSpecCharsEditorKit)实现的代码。

代码语言:javascript
复制
public class TextEditor extends JEditorPane {

    class UndoHandler implements UndoableEditListener {

        @Override
        public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
            undoAction.update();
            redoAction.update();
        }
    }

    class UndoAction extends AbstractAction {
        public UndoAction() {
            super("Undo");
            setEnabled(false);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("UNDO!");
            try {
                undoManager.undo();
            } catch (CannotUndoException ex) {
                // TODO deal with this
                ex.printStackTrace();
            }
            update();
            redoAction.update();
        }

        protected void update() {
            if (undoManager.canUndo()) {
                setEnabled(true);
                putValue(Action.NAME, undoManager.getUndoPresentationName());
            } else {
                setEnabled(false);
                putValue(Action.NAME, "Undo");
            }
        }
    }

    class RedoAction extends AbstractAction {
        public RedoAction() {
            super("Redo");
            setEnabled(false);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("REDO!");
            try {
                undoManager.redo();
            } catch (CannotRedoException ex) {
                ex.printStackTrace();
            }
            update();
            undoAction.update();
        }

        protected void update() {
            if (undoManager.canRedo()) {
                setEnabled(true);
                putValue(Action.NAME, undoManager.getRedoPresentationName());
            } else {
                setEnabled(false);
                putValue(Action.NAME, "Redo");
            }
        }
    }

    private UndoHandler undoHandler = new UndoHandler();
    private UndoManager undoManager = new UndoManager();
    private UndoAction undoAction = new UndoAction();
    private RedoAction redoAction = new RedoAction();

    public TextEditor() {
        super();
        // this.setEditorKit(new ShowSpecCharsEditorKit());

        this.getDocument().addUndoableEditListener(undoHandler);
        KeyStroke undoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
        KeyStroke redoKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);

        this.getInputMap().put(undoKeystroke, "undoKeystroke");
        this.getActionMap().put("undoKeystroke", undoAction);

        this.getInputMap().put(redoKeystroke, "redoKeystroke");
        this.getActionMap().put("redoKeystroke", redoAction);

         this.addCaretListener(new CaretListener() {

         @Override
         public void caretUpdate(CaretEvent e) {

        // ((EditorTab)getParent().getParent()).updateTabTitle(true);
         }
         });
    }

    @Override
    public void read(Reader r, Object desc) throws IOException {
        super.read(r, desc);
    }

    public static void main(String[] args) {
        JFrame jframe = new JFrame();
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setSize(500, 500);
        jframe.add(new TextEditor());
        jframe.setVisible(true);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39612658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档