想利用java来编一个记事本,复制剪切黏贴功能都很简单的实现了,就是用了textarea中的方法,可是 实现 撤销功能却遇到了困难。上网去查也没有得到答案,只是说有个 uodomanager什么,可是不知道该怎么用啊!哪位高手可以帮我解答一下。最好有一个示例!小弟,谢谢啦!
解决方案 »
- 搞不懂了,公用对象数组NullPointerException问题
- *****JAVA设置代理服务器访问网页(100分)*****
- 我刚装了netBeans4.1,在java类里中文显示乱码?
- 我的zip文件是用winrar压缩的,里面含有中文文件与中文目录。使用Java的zip压内的api去解压时,发生不能读取的中文的错误。请问谁知道怎
- 怎么在java程序中打开pdf文件
- BigDecimal类型判整除的问题,高手进来!
- 问一个关于输出的问题 用到了GregorianCalendar 谢谢~~
- 如何令窗体靠近到显示区边缘一定距离(比如10像素)就自动隐藏(就象QQ那样)?
- 终于挣够1000分了,辛苦了一个星期!
- 抽象类中能否定义static类型的成员变量,或是方法?如果能,有什么用呢?
- label 的repaint()问题
- 几道SQL笔试题目
你可以试试,即使是保存以后,仍然可以用ctrl+z来恢复保存前的内容,而且可以连续使用ctrl+z来恢复好几步操作的内容。
我觉得java中肯定有这样的组件。
一次ctrl+z后就出栈了,下次ctrl+z就是倒数第二次保存的内容如果要加上ctrl+y回归,则要再维护一个堆栈,ctrl+z将A栈内容压到B栈
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();
}
}
import java.awt.event.ActionEvent;import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
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.text.JTextComponent;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;public class Main {
public static void main(String[] argv) throws Exception {
JTextComponent textcomp = new JTextArea();
final UndoManager undo = new UndoManager();
Document doc = textcomp.getDocument(); JFrame f = new JFrame();
f.add(new JScrollPane(textcomp));
f.setSize(330, 300);
f.setVisible(true); doc.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent evt) {
undo.addEdit(evt.getEdit());
}
}); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canUndo()) {
undo.undo();
}
} catch (CannotUndoException e) {
}
}
}); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
public void actionPerformed(ActionEvent evt) {
try {
if (undo.canRedo()) {
undo.redo();
}
} catch (CannotRedoException e) {
}
}
}); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
}