关于记事本“撤销”这个按钮的功能有什么好的实现方法? 关于记事本“撤销”这个按钮的功能有什么好的实现方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 嗯,具体研究一下 设计模式的 Command模式。好运,古天乐~ 正在做用java做一个记事本,感觉这个键的功能很棘手!! 我做过, 你没次操作要用一个Command类来进行管理,然后把这些Command存到一个链表里。这样就差不多了。Command作为多种操作的Base class,应该提供redo和undo的接口方法。 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(); }} Java的Swing中不是提供了一个包装好的方法undo()~~~ Java中如何判断一个文件是否已经被写完了? 挺有意思的问题? 关于Java的IO与Socket问题(没有复杂的代码,只是一个用法的疑问,希望大家赏脸) jdbc 连接不成功,以下是提示内容,气死我了,求助!!! 我26岁了,有网络方面知识基础,刚开始学JAVA是否适合学习J2EE? 命令行下面不能运行.class程序了?? (Graphics2D)screen; java字符串反转 SOS:JAVA 中对树进行操作的问题---谈谈想法也可以了。! 比较对象大小== ,equals java实现堆排序的问题 账号和密码这种系统登录的方式,不安全
这样就差不多了。Command作为多种操作的Base class,应该提供redo和undo的接口方法。
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();
}
}