⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 undoableeditdemo.java

📁 《java事件处理指南》一书的代码,好东西
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;import javax.swing.undo.*;public class UndoableEditDemo extends JFrame                        implements UndoableEditListener, ActionListener{   private JTextArea jta;   protected UndoManager undoManager;   private JTextField jtf;   private JMenuBar menuBar;   private JMenu menu;   private JMenuItem undoItem, redoItem;   public UndoableEditDemo()    {/*  An UndoManager is created to manage undo and redo operations.   */      undoManager = new UndoManager();/*  A JTextArea is created and placed on a JFrame.  The Document  *//*  associated with the JTextArea is obtained.  The Document      *//*  registers an UndoableEventListener.                           */      jta = new JTextArea(5,15);      jta.setLineWrap(true);      jta.setWrapStyleWord(true);      jta.getDocument().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   *//*  since there is initially no text in the JTextArea.             */      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);      getContentPane().add(new JScrollPane(jta), 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 executes an undoable edit in the text        *//*  area, 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 perform undo");         }      }      if ( event.getActionCommand().equals("redo") )      {         try         {            undoManager.redo();            redoItem.setEnabled(undoManager.canRedo());            undoItem.setEnabled(undoManager.canUndo());         }         catch (CannotRedoException cre)          {            jtf.setText("Can't perform redo");         }      }   }   public static void main(String args[])    {      UndoableEditDemo demo = new UndoableEditDemo();   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -