java undo_使用Java来实现编辑器的Undo Redo功能

该博客介绍了如何在Java Swing中利用UndoManager为JEditorPane添加撤销/重做功能。通过创建UndoAction和RedoAction类,实现了键盘快捷键绑定以及编辑历史的管理,使得用户可以方便地进行文本编辑操作的撤销和重做。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值