2016-09-21 9 views
0

너무 오랫동안 싸워 왔기 때문에 누군가 도와 줄 수 있기를 바랍니다.(실행 취소/다시 실행 || KeyStrokes)이 JEditorPane에서 작동하지 않습니다.

내가 JEditorPane로 확장 클래스 (내가 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
귀하의 코드는 내가 코드에서

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

음 ... JTabbedPane 내의 JScrollPane 내에서 TextEditor를 사용하고 있습니다. 내 KeyStrokes가 다른 곳에서 등록하고있을 가능성이 있습니까? 즉, 다른 구성 요소가 듣고 있고 이러한 이벤트가 내 구성 요소에 떨어지지 않았을 가능성이 있습니까? – amateurjustin

+1

키 입력이 포커스 된 구성 요소에 등록됩니다. 포커스가있는 구성 요소에 수신기가 없으면 부모로 이동합니다. Texteditor가 청취자를 가지고 있기 때문에 귀하의 경우에는 다른 것으로 등록 된 것으로 생각하지 않습니다. –

+0

편집기 키트에 주석을 달았습니까? 또한 caretUpdate (나는 책임이 있다고 생각하지 않는다)를 주석하려고 노력한다. 내가 맞춤 편집기 키트와이 텍스트 편집기의 부모에게 코드를 주었다면 나는 실제 원인을 찾을 수있다. –