ITPub博客

首页 > 应用开发 > IT综合 > Adding Undo and Redo to a Text Component (转)

Adding Undo and Redo to a Text Component (转)

原创 IT综合 作者:amyz 时间:2007-10-29 10:00:54 0 删除 编辑
Adding Undo and Redo to a Text Component (转)[@more@]

 

These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms

Adding Undo and Redo to a Text Component

JTextArea textarea = new JTextArea(); final Undomanager undo = new UndoManager(); Document doc = textarea.getDocument(); //The next two lines should be in one line. doc.addUndoableEditListener( new UndoableEditListener() { The next two lines should be in one line. public void undoableEditHappened( UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); textarea.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textarea.getInputMap().put(KeyStroke.getKeyStroke( "control Z"), "Undo"); textarea.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textarea.getInputMap().put(KeyStroke.getKeyStroke( "control Y"), "Redo");


Examplets TM provided by peRmission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-979353/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3984
  • 访问量
    7334901