(撤消/重做|| KeyStrokes)在JEditorPane中不起作用

时间:2016-09-21 09:30:37

标签: java jeditorpane

我希望有人可以提供帮助,因为我已经与此争斗太久而无法解决问题。

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

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);
}

}

由于某些原因,我的击键没有被解雇或撤消/重做只是没有工作。

我无法让它发挥作用。有人可能会向我指出一些问题吗?

1 个答案:

答案 0 :(得分:1)

当我评论

时,您的代码似乎工作正常 来自您代码的

this.setEditorKit(new ShowSpecCharsEditorKit());

这可能是编辑器工具包的问题,​​请检查关键操作和操作的自定义EditorKit(ShowSpecCharsEditorKit)实现的代码。

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);
    }
}