日期:2014-05-20  浏览次数:21039 次

java如何实现记事本中的 撤销 功能
想利用java来编一个记事本,复制剪切黏贴功能都很简单的实现了,就是用了textarea中的方法,可是 实现 撤销功能却遇到了困难。上网去查也没有得到答案,只是说有个 uodomanager什么,可是不知道该怎么用啊!哪位高手可以帮我解答一下。最好有一个示例!小弟,谢谢啦!

------解决方案--------------------
用一个栈,存放用户的操作记录
------解决方案--------------------
如果没有专门的方法,你自己可以弄个看看,设一个缓冲区,,,,,冒似比较复杂。。
------解决方案--------------------
把最后一个操作放到缓冲里面,如果保存就写入,撤销就删除
------解决方案--------------------
java swing 有这个控件 叫UodoManager 试试 好了
------解决方案--------------------
JTextPane的有专门的撤销监听吧,与 UndoableEditListener 有关


------解决方案--------------------
顶1楼,自己写一个,不麻烦的,就是一个缓存。
------解决方案--------------------
探讨
顶1楼,自己写一个,不麻烦的,就是一个缓存。

------解决方案--------------------
探讨
引用:
顶1楼,自己写一个,不麻烦的,就是一个缓存。

没这么简单的。
你可以试试,即使是保存以后,仍然可以用ctrl+z来恢复保存前的内容,而且可以连续使用ctrl+z来恢复好几步操作的内容。
我觉得java中肯定有这样的组件。


------解决方案--------------------

Java code
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

public class UndoRedoTextArea extends JFrame {
    protected JTextArea textArea = new JTextArea();

    protected UndoManager undoManager = new UndoManager();

    protected JButton undoButton = new JButton("Undo");

    protected JButton redoButton = new JButton("Redo");

    public UndoRedoTextArea() {
        super("Undo/Redo Demo");

        undoButton.setEnabled(false);
        redoButton.setEnabled(false);

        JPanel buttonPanel = new JPanel(new GridLayout());
        buttonPanel.add(undoButton);
        buttonPanel.add(redoButton);

        JScrollPane scroller = new JScrollPane(textArea);

        getContentPane().add(buttonPanel, BorderLayout.NORTH);
        getContentPane().add(scroller, BorderLayout.CENTER);

        textArea.getDocument().addUndoableEditListener(
                new UndoableEditListener() {
                    public void undoableEditHappened(UndoableEditEvent e) {
                        undoManager.addEdit(e.getEdit());
                        updateButtons();
                    }
                });

        undoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    undoManager.undo();
                } catch (CannotRedoException cre) {
                    cre.printStackTrace();
                }
                updateButtons();
            }
        });

        redoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    undoManager.redo();
                } catch (CannotRedoException cre) {
                    cre.printStackTrace();
                }
                updateButtons();
            }
        });

        setSize(400, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void updateButtons() {
        undoButton.setText(undoManager.getUndoPresentationName());
        redoButton.setText(undoManager.getRedoPresentationName());
        undoButton.setEnabled(undoManager.canUndo());
        redoButton.setEnabled(undoManager.canRedo());
    }

    public static void main(String argv[]) {
        new UndoRedoTextArea();
    }
}