1. 程式人生 > >JAVA中的Redo與Undo功能的程式設計

JAVA中的Redo與Undo功能的程式設計

在Java中,Package: javax.swing.undo是專門用來 撤消與重做的包.

簡單使用的基本步驟如下:

1) 實現UndoableEditListener介面中的方法

  public void undoableEditHappened(UndoableEditEvent e) {
              Model.addEdit(e.getEdit());    基中Model為UndoManager的例項.
  }

2) 在對應的編輯器的Document中註冊如下:

  mEditPanel.getDocument().addUndoableEditListener((UndoableEditListener)pDocumentListener);

 其中pDocumentListener為實現了UndoableEditListener介面類的例項.

3) 給對應的按鈕編寫事件,如下:

      else if(strCommand.equals("Redo")){
      try{
        um.redo();
      }catch(Exception er){}
    }
    else if(strCommand.equals("Undo")){
      try{
        um.undo();
   }catch(Exception er){}
    }    //注意如果不能撤消或者不能重做時,throw  CannotUndoException

 .

      這裡只是Undo包的初級使用,如果要使用這個包的高階功能,還得撐握包中一些類的使用.