java如何实现记事本中的 撤销 功能 想利用java来编一个记事本,复制剪切黏贴功能都很简单的实现了,就是用了textarea中的方法,可是 实现 撤销功能却遇到了困难。上网去查也没有得到答案,只是说有个 uodomanager什么,可是不知道该怎么用啊!哪位高手可以帮我解答一下。最好有一个示例!小弟,谢谢啦! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java swing 有这个控件 叫UodoManager 试试 好了 JTextPane的有专门的撤销监听吧,与 UndoableEditListener 有关 没这么简单的。你可以试试,即使是保存以后,仍然可以用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(); }} 再放一个,按Ctrl+Z撤消的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"); }} 不错的代码喔、、只是在TextArea里不行额、、、、 如何用javascript处理下面的字符串 01/03/2009为日期类型? 关于线程安全的一个问题 swing 相关资料(中文) eclipse中如何要两个工程之间调用 如何用java实现硬盘文件搜索,谢谢 用JNI实现C++调JAVA,类路径的设置问题 请问哪里可以打包下载Java的api:j2se/1.4.1/docs/api/index.html 介绍一些学习JAVA SWING 方面的好书. 请教个简单的问题。谢谢 各位:你们用什么操作系统做JAVA服务器?WIN2000?LINUX?SOLARIS? 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"); }
}