java文本的撤销和恢复 您所在的位置:网站首页 word的撤销与恢复 java文本的撤销和恢复

java文本的撤销和恢复

2024-02-17 17:23| 来源: 网络整理| 查看: 265

文本的撤销和恢复是通过 addUndoableEditListener(UndoableEditListener listener)这个方法来注册实现的。只要是Document类及其子类都可以注册撤销和恢复的监听来实现文档的撤销和恢复,这是非常容易实现的。所以JTextField,JTextArea,JTextPane都可以实现撤销和恢复功能。因为他们都可以获得Document实例,通过这个方法----getDocument();下面来用实例来讲解一下。

下面的实例是在一个JTextPane中实现撤销和恢复,通过右键弹出菜单来操作。弹出菜单的第一个菜单项是"撤销",第二个是"恢复",第三个是"插入图片"。

 

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.JTextPane;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.undo.UndoManager;import com.jijing.tool.SwingConsole;

public class UndoFunction extends JFrame{

    /**     * @param args     * 实现简单的撤销功能,并通过Ctrl+Z快捷键来快速操作,这次是在一个文本面板中,既可以写文字又可以插入图片,可以撤销也可以恢复     * 通过鼠标右键来弹出菜单实现撤销和恢复的操作     */    private UndoManager um;//撤销管理类    private JTextPane jp;//文本面板    private String name[]={            "撤销",            "恢复",            "插入图片"    };    private JPopupMenu pm;//右键弹出菜单类    private JMenuItem mt[];    public UndoFunction(){        um=new UndoManager();        jp=new JTextPane();        pm=new JPopupMenu();        mt=new JMenuItem[3];        for(int i=0;i            public void mousePressed(MouseEvent e){                maybeShowPopup(e);            }            public void mouseReleased(MouseEvent e){                maybeShowPopup(e);            }            private void maybeShowPopup(MouseEvent e){                if(e.isPopupTrigger()){//如果有弹出菜单                    pm.show(e.getComponent(), e.getX(), e.getY());                }            }        });        jp.getDocument().addUndoableEditListener(new UndoableEditListener(){//注册撤销可编辑监听器            public void undoableEditHappened(UndoableEditEvent e) {                um.addEdit(e.getEdit());            }                    });//编辑撤销的监听    }    public static void main(String[] args) {        SwingConsole.swingRun(new UndoFunction(),600,400);    }    class PopupAction implements ActionListener{        public void actionPerformed(ActionEvent e) {            if(e.getSource()==mt[0]&&um.canUndo()){//撤销                um.undo();            }else if(e.getSource()==mt[1]&&um.canRedo()){//恢复                um.redo();            }else if(e.getSource()==mt[2]){//插入图片                ImageIcon icon=new ImageIcon(getClass().getResource("/1.png"));                jp.insertIcon(icon);            }        }    }}

其实对于文档编辑的撤销和恢复是非常简单的,只要获取getDocument()就可以实现监听了,在监听方法中添加编辑数据就可以了,

UndoManager .addUndoableEditListener(UndoableEditEvent .getEdit());

在就是在撤销操作中调用UndoManager .undo()就可以了,还有canUndo()这个方法和重要,用于判断undo操作是否成功,如果成功就返回true。

在恢复操作中调用UndoManager .redo()可以实现恢复,还有canRedo()方法判断redo操作是否成功,如果成功返回true。

//下面讲解的是 如果没有提供addUndoableEditListener()方法怎么实现撤销和恢复操作



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有