2016-09-21 24 views
0

我希望有人能夠幫助,因爲我一直在與此爭鬥太久,無法擺脫困境。(Undo/Redo || 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); 
} 

} 

出於某種原因,我的按鍵都沒有被解僱或撤銷/重做只是ISN」不工作。

我不能得到它的工作。請問某人可能會向我指出什麼?在按鍵

回答

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(我不認爲它是負責任的,雖然)我可以找到實際的原因,如果你給我的代碼到你的自定義編輯器套件和這個texteditor的父母 –

相關問題