📄 undoablesuppdemo.java
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;import javax.swing.undo.*;public class UndoableSuppDemo extends JFrame implements UndoableEditListener, ActionListener{ private UndoableJCheckBox jcb; protected UndoManager undoManager; private JTextField jtf; private JMenuBar menuBar; private JMenu menu; private JMenuItem undoItem, redoItem; public UndoableSuppDemo() {/* An UndoManager is created to manage undo and redo operations. */ undoManager = new UndoManager();/* An UndoableJCheckBox is created and placed on a JFrame. *//* Because the UndoableJCheckBox class uses an UndoableEditSupport *//* object, an UndoableJCheckBox object can be registered with an *//* UndoableEventListener. */ jcb = new UndoableJCheckBox("Jackson"); jcb.addUndoableEditListener(this); jtf = new JTextField(15); jtf.setEditable(false);/* An "Edit" menu is created with "undo" and "redo" menu items. *//* These menu items will initiate undo and redo actions to the *//* text in the text area. The menu items are initially disabled *//* until the user interacts with the UndoableJCheckBox object. */ undoItem = new JMenuItem("undo"); undoItem.setEnabled(false); undoItem.addActionListener(this); redoItem = new JMenuItem("redo"); redoItem.setEnabled(false); redoItem.addActionListener(this); menu = new JMenu("Edit"); menu.add(undoItem); menu.add(redoItem); menuBar = new JMenuBar(); menuBar.add(menu); JPanel panel = new JPanel(); panel.add(jcb); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().add(menuBar, BorderLayout.NORTH); getContentPane().add(jtf, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 300, 250); setVisible(true); }/* The UndoableEditDemo class serves as the UndoableEditListener, *//* so it provides an implementation of the undoableEditHappened() *//* method. When the user checks or un-checks the UndoableJCheckBox, *//* an UndoableEditEvent is generated and sent to this method. *//* The method adds the edit to the list managed by the UndoManager. *//* The "undo" and "redo" menu items are enabled or disabled depending *//* on whether subsequent undo and redo operations are possible. */ public void undoableEditHappened(UndoableEditEvent event) { undoManager.addEdit(event.getEdit()); redoItem.setEnabled(undoManager.canRedo()); undoItem.setEnabled(undoManager.canUndo()); }/* The actionPerformed() method responds to the selection of the *//* "undo" and "redo" menu items. If the "undo" menu item was *//* selected, the UndoManager attempts to undo the last undoable *//* edit in its list. If the "redo" menu item was chosen, the *//* UndoManager tries to redo (undo the undo). After the actions *//* are taken, the menu items are enabled or disabled depending *//* on whether subsequent undo and redo operations are possible. */ public void actionPerformed(ActionEvent event) { if ( event.getActionCommand().equals("undo") ) { try { undoManager.undo(); redoItem.setEnabled(undoManager.canRedo()); undoItem.setEnabled(undoManager.canUndo()); } catch (CannotUndoException cue) { jtf.setText("can't undo"); } } if ( event.getActionCommand().equals("redo") ) { try { undoManager.redo(); redoItem.setEnabled(undoManager.canRedo()); undoItem.setEnabled(undoManager.canUndo()); } catch (CannotRedoException cre) { jtf.setText("can't redo"); } } } public static void main(String args[]) { UndoableSuppDemo demo = new UndoableSuppDemo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -