程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 用java實現編輯器的撤消、重做功能

用java實現編輯器的撤消、重做功能

編輯:關於JAVA

用java實現編輯器的撤消、重做功能,非常的方便,下面是一個實現這個功能的類,

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JEditorPane;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
/**
* UndoWrapper is responsible for adding undo and redo support to text components.
* @author Antonio Vieiro ([email protected]), $Author: $
* @version $Revision: $
*/
public class UndoWrapper
  implements UndoableEditListener
{
  private UndoManager undoManager;
  private UndoAction undoAction;
  private RedoAction redoAction;
  private JEditorPane textComponent;

  /**
  * Creates a new instance of UndoWrapper
  */
  public UndoWrapper( JEditorPane aComponent )
  {
   textComponent = aComponent;
   undoManager = new UndoManager();
   undoAction = new UndoAction();
   redoAction = new RedoAction();
   textComponent.getDocument().addUndoableEditListener( this );
   textComponent.getInputMap().put( (KeyStroke) undoAction.getValue(
Action.ACCELERATOR_KEY), "undo" );
   textComponent.getInputMap().put( (KeyStroke) redoAction.getValue(
Action.ACCELERATOR_KEY), "redo" );
   textComponent.getActionMap().put( "undo", undoAction );
   textComponent.getActionMap().put( "redo", redoAction );
  }

  public void undoableEditHappened(UndoableEditEvent e)
  {
   undoManager.addEdit( e.getEdit() );
   undoAction.updateUndoState();
   redoAction.updateRedoState();
  }

  /**
  * UndoAction is the Action responsible for handling the undo operation.
  */
  class UndoAction
   extends AbstractAction
  {
   public UndoAction()
   {
    super( "Cannot undo" ); // TODO: I18N
    setEnabled( false );
    putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Z") );
   }

   public void actionPerformed(ActionEvent e)
   {
    try
    {
     undoManager.undo();
    }
    catch( CannotUndoException cue )
    {
     // TODO: Use logging?
     cue.printStackTrace( System.err );
    }
    updateUndoState();
    redoAction.updateRedoState();
   }

   void updateUndoState()
   {
    if ( undoManager.canUndo() )
    {
     setEnabled( true );
     putValue( Action.NAME, "Undo" ); // TODO I18N
    }
    else
    {
     setEnabled( false );
     putValue( Action.NAME, "Cannot undo" ); // TODO I18N
    }
   }
  }

  /**
  * RedoAction is the Action responsible for handling the redo operation.
  */
  class RedoAction
   extends AbstractAction
  {
   public RedoAction()
   {
    super( "Cannot redo" ); // TODO I18N
    setEnabled( false );
    putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Y") );
   }
   public void actionPerformed(ActionEvent e)
   {
    try
    {
     undoManager.redo();
    }
    catch( CannotRedoException cre )
    {
     // TODO: Use logging?
     cre.printStackTrace( System.err );
    }
    updateRedoState();
    undoAction.updateUndoState();
   }

   void updateRedoState()
   {
    if ( undoManager.canRedo() )
    {
     setEnabled( true );
     putValue( Action.NAME, "Redo" ); // TODO I18N
    }
    else
    {
     setEnabled( false );
     putValue( Action.NAME, "Cannot redo" ); // TODO I18N
    }
   }
  }

  UndoAction getUndoAction()
  {
   return undoAction;
  }

  RedoAction getRedoAction()
  {
   return redoAction;
  }
}

使用的時候,只需要將你創建的JEditorPane作為對象傳入UndoWrapper中即可。使用方式如下new UndoWrapper(editorPane);

OK這樣你的編輯器就具有了Undo Redo功能,而且是次數不收限制的。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved