当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:12:00  来源:本站整理

用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 (antonio@antonioshome.net), $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功效,并且是次数不收限制的.


  以上是“用java实现编辑器的撤消、重做功效[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • <b>如安在Oracle中利用Java存储历程</b>
  • 用Java实现自动在数据库表中生成ID号
  • 利用javascript获得浏览器中的星号密码办法
  • 用javabean来实现MySQL的分页显示
  • 用Java 1.1 AWT制作窗口和程序片
  • 用Java ME举行无线消息传送
  • <b>用Java筹划COM服务器</b>
  • 用Java筹划COM客户
  • <b>用Java程序生成文本的捷径</b>
  • 用Java实现FTP服务器办理策划
  • 用Java实现多线程服务器程序
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .