Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > java编辑组件的撤消与恢复操作的实现
【标  题】:java编辑组件的撤消与恢复操作的实现
【关键字】:java
【来  源】:http://blog.csdn.net/shaot399/archive/2007/02/16/1511014.aspx

java编辑组件的撤消与恢复操作的实现

Your Ad Here

package test;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.UndoManager;

public class UndoRedoTest {
 
 JFrame tJFrame;
 JTextArea tJTextArea;
 
 UndoManager undo = new UndoManager();
 Document doc = null;
 
 /**
  * 建立用户界面
  *
  */
 public void BuildUI(){
  tJFrame=new JFrame();
  tJTextArea=new JTextArea();
  
  tJTextArea.setSize(200,200);
  
  tJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  tJFrame.setSize(500,350);
  tJFrame.setVisible(true);
  tJFrame.add(tJTextArea);
  
  doc=tJTextArea.getDocument();
  
  undoRedoDefine();
 }
 /**
  * 撤消恢复相关定义
  */
 public void undoRedoDefine(){
  doc.addUndoableEditListener(
    new UndoableEditListener(){
     public void undoableEditHappened(UndoableEditEvent arg0) {
      // TODO Auto-generated method stub
      doc_addEdit(arg0);
     }
    }
  );
  addActionMap();//添加撤消与恢复所需的定义
 }
 public void addActionMap(){
  tJTextArea.getActionMap().put("Undo",new AbstractAction("Undo"){

   public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if(undo.canUndo()){
     undo.undo();
    }
   }
   
  });
  tJTextArea.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
  
  tJTextArea.getActionMap().put("Redo",new AbstractAction("Redo"){

   public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    if(undo.canRedo()){
     undo.redo();
    }
   }
   
  });
  tJTextArea.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
 }
 public void doc_addEdit(UndoableEditEvent e){
  undo.addEdit(e.getEdit());
 }
 /**
  * 主函数
  * @param args
  */
 public static void main(String args[]){
  
  UndoRedoTest urt=new UndoRedoTest();
  urt.BuildUI();
 }
}
 

批量文件重命名器BatchRename程序:【上一篇】
MS SQL Server 2005 开发之分页存储过程:【下一篇】
【相关文章】
  • 防止JAVA SWING 多重启动的最好方法.
  • Java异常处理的陋习
  • 一段有关JavaScript的翻译
  • 如何用JavaScript去触发Button的Click事件
  • XMLBEANS 2.0 —— 一位JAVA开发人员的视点
  • Java日志工具中获取文件信息的原理
  • 如何使用JavaScript和正则表达式进行数据验证
  • javascript 正则表达式祥解
  • Ajax陷阱-JavaScript篇-[更新:2007-2-14]
  • 55种网页常用技巧(javascript)
  • 【随机文章】
  • Make使用方法
  • 三種實現 ListView 欄位排序指標(Up/Down Arrow)的方法
  • 声道(音箱)如何播放. 对单声道声
  • Pro Hibernate 3笔记和小结(3)之第一章Hibernate入门
  • Maya UV 编辑实例系列(8)NURBS表面方向
  • Object类的用法(三)
  • 康熙微微一笑
  • 关于SetTimer函数!
  • 架构设计中的方法学(1)——方法源于恐惧(2)
  • 使用usb闪存所用到的模块及挂载时可能出现的错误
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.