游客:
注册
|
登录
|
帮助
网友俱乐部
»
网络编程
»
JAVA教程
» 使用Java来实现编辑器的Undo Redo功能
网友生活网
|
网友学堂
如何获得金币?
诚招斑竹
IP归属地查询 PR查询 收录查询 whois查询
免费超短2级域名,your.jpy.cc
传世私服
|
传奇世界私服
注册亚洲交友中心,找个人来同居吧!
免费存储空间,免费网络硬盘
‹‹ 上一主题
|
下一主题 ››
投票
交易
悬赏
活动
打印
|
推荐
|
订阅
|
收藏
标题:
[基础]
使用Java来实现编辑器的Undo Redo功能
ljjk5
元帅
UID 46706
精华
1
积分 99426
帖子 49690
威望 554
金币 48489
热心 505
阅读权限 100
注册 2007-2-25
状态 离线
#1
使用道具
发表于 2008-1-6 14:44
资料
个人空间
主页
短消息
加为好友
使用Java来实现编辑器的Undo Redo功能
用java实现编辑器的Undo Redo功能,非常的方便,下面是一个实现这个功能的类,
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功能,而且是次数不收限制的。
网友
ljjk5
签名 - 网友社区
===
[广告]
免费域名(Free Subdomain)
免费空间(Free hosting)
PR查询(Google Pagerank)
投票
交易
悬赏
活动
控制面板首页
编辑个人资料
积分交易
公众用户组
好友列表
个人空间管理
基本概况
流量统计
客户软件
发帖量记录
论坛排行
主题排行
发帖排行
积分排行
在线时间
管理团队
管理统计
当前时区 GMT+8, 现在时间是 2008-10-12 09:19
信产部ICP备案:
京ICP备05066424号
北京市公安局网监备案:1101050648号
Powered by
Discuz!
5.5.0
TOP
清除 Cookies
-
联系我们
-
网友俱乐部
-
Archiver
-
WAP