|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最后就是我对java的几点希望:首先是IDE工具,有人说java已经很好了,有jbuilder,eclipse,netBeans等等,但是我认为如果java想超越.net,那么他首先要解决的就是IDE工具的整合。
用java完成编纂器的取消、重做功效,十分的便利,上面是一个完成这个功效的类,
importjava.awt.event.ActionEvent;
importjavax.swing.AbstractAction;
importjavax.swing.Action;
importjavax.swing.JEditorPane;
importjavax.swing.KeyStroke;
importjavax.swing.event.UndoableEditEvent;
importjavax.swing.event.UndoableEditListener;
importjavax.swing.text.JTextComponent;
importjavax.swing.undo.CannotRedoException;
importjavax.swing.undo.CannotUndoException;
importjavax.swing.undo.UndoManager;
/**
*UndoWrapperisresponsibleforaddingundoandredosupporttotextcomponents.
*@authorAntonioVieiro(antonio@antonioshome.net),$Author:$
*@version$Revision:$
*/
publicclassUndoWrapper
implementsUndoableEditListener
{
privateUndoManagerundoManager;
privateUndoActionundoAction;
privateRedoActionredoAction;
privateJEditorPanetextComponent;
/**
*CreatesanewinstanceofUndoWrapper
*/
publicUndoWrapper(JEditorPaneaComponent)
{
textComponent=aComponent;
undoManager=newUndoManager();
undoAction=newUndoAction();
redoAction=newRedoAction();
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);
}
publicvoidundoableEditHappened(UndoableEditEvente)
{
undoManager.addEdit(e.getEdit());
undoAction.updateUndoState();
redoAction.updateRedoState();
}
/**
*UndoActionistheActionresponsibleforhandlingtheundooperation.
*/
classUndoAction
extendsAbstractAction
{
publicUndoAction()
{
super("Cannotundo");//TODO:I18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke("ctrlZ"));
}
publicvoidactionPerformed(ActionEvente)
{
try
{
undoManager.undo();
}
catch(CannotUndoExceptioncue)
{
//TODO:Uselogging?
cue.printStackTrace(System.err);
}
updateUndoState();
redoAction.updateRedoState();
}
voidupdateUndoState()
{
if(undoManager.canUndo())
{
setEnabled(true);
putValue(Action.NAME,"Undo");//TODOI18N
}
else
{
setEnabled(false);
putValue(Action.NAME,"Cannotundo"); //TODOI18N
}
}
}
/**
*RedoActionistheActionresponsibleforhandlingtheredooperation.
*/
classRedoAction
extendsAbstractAction
{
publicRedoAction()
{
super("Cannotredo"); //TODOI18N
setEnabled(false);
putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke("ctrlY"));
}
publicvoidactionPerformed(ActionEvente)
{
try
{
undoManager.redo();
}
catch(CannotRedoExceptioncre)
{
//TODO:Uselogging?
cre.printStackTrace(System.err);
}
updateRedoState();
undoAction.updateUndoState();
}
voidupdateRedoState()
{
if(undoManager.canRedo())
{
setEnabled(true);
putValue(Action.NAME,"Redo"); //TODOI18N
}
else
{
setEnabled(false);
putValue(Action.NAME,"Cannotredo"); //TODOI18N
}
}
}
UndoActiongetUndoAction()
{
returnundoAction;
}
RedoActiongetRedoAction()
{
returnredoAction;
}
}
利用的时分,只必要将你创立的JEditorPane作为对象传进UndoWrapper中便可。利用体例以下newUndoWrapper(editorPane);
OK如许你的编纂器就具有了UndoRedo功效,并且是次数不收限定的。
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。 |
|