swing 文本域的undo和右键开始菜单

简介:

1,让文本域可以undo,比如支持Ctrl+Z,Ctrl+Y,Ctrl+S(联想windows的notepad)

下面是一个可以undo的文本域:

Java代码   收藏代码
  1. package com.swing.component;  
  2.   
  3. import java.awt.event.ActionEvent;  
  4.   
  5. import javax.swing.AbstractAction;  
  6. import javax.swing.JTextArea;  
  7. import javax.swing.KeyStroke;  
  8. import javax.swing.event.UndoableEditEvent;  
  9. import javax.swing.event.UndoableEditListener;  
  10. import javax.swing.text.Document;  
  11. import javax.swing.undo.CannotRedoException;  
  12. import javax.swing.undo.CannotUndoException;  
  13. import javax.swing.undo.UndoManager;  
  14.   
  15. public class UndoTextArea extends JTextArea {  
  16.     private static final long serialVersionUID = 2622113838910292609L;  
  17.     private UndoManager undo = new UndoManager();  
  18.     private Document doc = getDocument();  
  19.     private StringBuffer stringbuf = null;  
  20.   
  21.     public void stopUndo() {  
  22.         // undo.die();  
  23.         undo.discardAllEdits();  
  24.     }  
  25.   
  26.     public UndoManager getUndo() {  
  27.         return undo;  
  28.     }  
  29.   
  30.     public void setUndo(UndoManager undo) {  
  31.         this.undo = undo;  
  32.     }  
  33.   
  34.     public Document getDoc() {  
  35.         return doc;  
  36.     }  
  37.   
  38.     public void setDoc(Document doc) {  
  39.         this.doc = doc;  
  40.     }  
  41.   
  42.     /** 
  43.      * @autho : whuang dicate original content of the area 
  44.      * @return 
  45.      */  
  46.     public StringBuffer getStringbuf() {  
  47.         return stringbuf;  
  48.     }  
  49.   
  50.     /*** 
  51.      * dicate original content of the area 
  52.      *  
  53.      * @param stringbuf 
  54.      */  
  55.     public void setStringbuf(StringBuffer stringbuf) {  
  56.         this.stringbuf = stringbuf;  
  57.     }  
  58.   
  59.     private void initlize() {  
  60.         doc.addUndoableEditListener(new UndoableEditListener() {  
  61.             public void undoableEditHappened(UndoableEditEvent e) {  
  62.                 undo.addEdit(e.getEdit());  
  63.             }  
  64.         });  
  65.         addActionMap();  
  66.     }  
  67.   
  68.     public UndoTextArea() {  
  69.         initlize();  
  70.   
  71.     }  
  72.   
  73.     public UndoTextArea(int rows, int columns) {  
  74.         super(rows, columns);  
  75.         initlize();  
  76.   
  77.     }  
  78.   
  79.     public void addActionMap() {  
  80.         getActionMap().put("Undo"new AbstractAction("Undo11") {  
  81.             private static final long serialVersionUID = 2434402629308759912L;  
  82.   
  83.             public void actionPerformed(ActionEvent evt) {  
  84.                 try {  
  85.                     boolean b = undo.canUndo();  
  86.                     // System.out.println("whether undo : "+b);  
  87.                     if (b) {  
  88.                         undo.undo();  
  89.                     }  
  90.                 } catch (CannotUndoException e) {  
  91.                 }  
  92.             }  
  93.         });  
  94.         getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");  
  95.   
  96.         getActionMap().put("Redo"new AbstractAction("Redo1111") {  
  97.             private static final long serialVersionUID = 5348330289578410517L;  
  98.   
  99.             public void actionPerformed(ActionEvent evt) {  
  100.                 try {  
  101.                     if (undo.canRedo()) {  
  102.                         undo.redo();  
  103.                     }  
  104.                 } catch (CannotRedoException e) {  
  105.                 }  
  106.             }  
  107.         });  
  108.         getInputMap().put(KeyStroke.getKeyStroke("control R"), "Redo");  
  109.   
  110.         getActionMap().put("Copy"new AbstractAction("Copy111") {  
  111.             private static final long serialVersionUID = -5151480809625853288L;  
  112.   
  113.             public void actionPerformed(ActionEvent evt) {  
  114.                 copy();  
  115.             }  
  116.   
  117.         });  
  118.         getInputMap().put(KeyStroke.getKeyStroke("control C"), "Copy");  
  119.   
  120.         getActionMap().put("Cut"new AbstractAction("Cut") {  
  121.   
  122.             private static final long serialVersionUID = 7316612864835857713L;  
  123.   
  124.             public void actionPerformed(ActionEvent evt) {  
  125.                 cut();  
  126.             }  
  127.   
  128.         });  
  129.   
  130.         getInputMap().put(KeyStroke.getKeyStroke("control X"), "Cut");  
  131.   
  132.         getActionMap().put("Paste"new AbstractAction("Paste111") {  
  133.             private static final long serialVersionUID = -3548620001691220571L;  
  134.   
  135.             public void actionPerformed(ActionEvent evt) {  
  136.                 paste();  
  137.             }  
  138.         });  
  139.   
  140.         getInputMap().put(KeyStroke.getKeyStroke("control V"), "Paste");  
  141.   
  142.         // redo Ctrl + Y  
  143.         getActionMap().put("Redo"new AbstractAction("reDo111") {  
  144.             private static final long serialVersionUID = -3548620001691220571L;  
  145.   
  146.             public void actionPerformed(ActionEvent evt) {  
  147.                 if (undo.canRedo()) {  
  148.                     undo.redo();  
  149.                 }  
  150.             }  
  151.         });  
  152.   
  153.         getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");  
  154.   
  155.     }  
  156.   
  157. }  

 示例:

Java代码   收藏代码
  1. inputTextArea = new UndoTextArea();  
  2.         inputTextArea.setLineWrap(true);  
  3.         inputTextArea.setWrapStyleWord(true);  

 

(2)增加右键菜单

效果如下:



 
 代码如下:

Java代码   收藏代码
  1. package com.swing.menu;  
  2.   
  3. import java.awt.event.MouseEvent;  
  4.   
  5. import javax.swing.JMenuItem;  
  6. import javax.swing.JPopupMenu;  
  7. import javax.swing.JTextField;  
  8. import javax.swing.event.MouseInputListener;  
  9. import javax.swing.text.JTextComponent;  
  10.   
  11. public class MenuUtil2  
  12. {  
  13.     public static final String ACTION_STR_OPEN               = "open";  
  14.     public static final String ACTION_STR_DELETE_FILE        = "delete file";  
  15.     public static final String ACTION_STR_EXIT               = "exit";  
  16.     public static final String ACTION_STR_BROWSER            = "browser";  
  17.     public static final String ACTION_STR_COPY               = "copy";  
  18.     public static final String ACTION_STR_COPY_ALL           = "copy all";  
  19.     public static final String ACTION_STR_PASTE              = "paste";  
  20.     public static final String ACTION_STR_REPLACE_ALL              = "replace all";  
  21.     public static final String ACTION_STR_DELETE_CONTENT     = "delete";  
  22.     public static final String ACTION_STR_DELETE_ALL_CONTENT = "delete all";  
  23.     public static final String ACTION_STR_SELECT_ALL_CONTENT = "select all";  
  24.     public static final String ACTION_STR_REFRESH            = "refresh";  
  25.     public static final String ACTION_STR_COPY_FILEPATH      = "copy file path";  
  26.     public static final String ACTION_STR_FILE_RENAME        = "rename";  
  27.     public static final String ACTION_STR_CLOSE              = "close";  
  28.     public static final String ACTION_STR_NEW                = "new";  
  29.     public static final String ACTION_STR_ADD                = "add";  
  30.     public static final String ACTION_STR_SAVE               = "save";  
  31.     public static final String ACTION_STR_EDIT               = "edit";  
  32.     public static final String ACTION_STR_VIEW               = "view";  
  33.     public static final String ACTION_STR_UPDATE             = "update";  
  34.     public static final String ACTION_STR_INSERT             = "insert";  
  35.       
  36.     private MenuUtil2()  
  37.     {  
  38.         throw new Error("Don't let anyone instantiate this class.");  
  39.     }  
  40.     /*** 
  41.      * 给文本框增加右键菜单. 
  42.      *  
  43.      * @param field2 
  44.      */  
  45.     public static void setPopupMenu(final JTextComponent field2)  
  46.     {  
  47.         field2.addMouseListener(new MouseInputListener()  
  48.         {  
  49.             @Override  
  50.             public void mouseMoved(MouseEvent e)  
  51.             {  
  52.   
  53.             }  
  54.   
  55.             @Override  
  56.             public void mouseDragged(MouseEvent e)  
  57.             {  
  58.   
  59.             }  
  60.   
  61.             @Override  
  62.             public void mouseReleased(MouseEvent e)  
  63.             {  
  64.                 //                super.mousePressed(e);  
  65.                 if (e.getButton() == MouseEvent.BUTTON3)  
  66.                 {  
  67.                     JPopupMenu textMenu = new JPopupMenu();  
  68.                     JMenuItem copyM = new JMenuItem(MenuUtil2.ACTION_STR_COPY);  
  69.                     JMenuItem copyAllM = new JMenuItem(  
  70.                         MenuUtil2.ACTION_STR_COPY_ALL);  
  71.                     JMenuItem pasteM = new JMenuItem(MenuUtil2.ACTION_STR_PASTE);  
  72.                     JMenuItem replaceAllM = new JMenuItem(MenuUtil2.ACTION_STR_REPLACE_ALL);  
  73.                     JMenuItem deleteM = new JMenuItem(  
  74.                         MenuUtil2.ACTION_STR_DELETE_CONTENT);  
  75.                     JMenuItem deleteAllM = new JMenuItem(  
  76.                         MenuUtil2.ACTION_STR_DELETE_ALL_CONTENT);  
  77.                     JMenuItem selAllM = new JMenuItem(  
  78.                         MenuUtil2.ACTION_STR_SELECT_ALL_CONTENT);  
  79.                     Menu2ActionListener myMenuListener=new Menu2ActionListener(field2);  
  80.                     copyM.addActionListener(myMenuListener);  
  81.                     copyAllM.addActionListener(myMenuListener);  
  82.                     pasteM.addActionListener(myMenuListener);  
  83.                     replaceAllM.addActionListener(myMenuListener);  
  84.                     deleteM.addActionListener(myMenuListener);  
  85.                     deleteAllM.addActionListener(myMenuListener);  
  86.                     selAllM.addActionListener(myMenuListener);  
  87.                     textMenu.add(copyM);  
  88.                     textMenu.add(copyAllM);  
  89.                     if(!(field2 instanceof JTextField)){  
  90.                         /*因为JTextField 没有 ta2.insert(content, caret) 方法 .*/  
  91.                          textMenu.add(pasteM);  
  92.                     }  
  93.                      
  94.                     textMenu.add(replaceAllM);  
  95.                     textMenu.add(deleteM);  
  96.                     textMenu.add(deleteAllM);  
  97.                     textMenu.add(selAllM);  
  98.                     textMenu.show(e.getComponent(), e.getX(), e.getY());  
  99.                 }  
  100.             }  
  101.   
  102.             @Override  
  103.             public void mousePressed(MouseEvent e)  
  104.             {  
  105.             }  
  106.   
  107.             @Override  
  108.             public void mouseExited(MouseEvent e)  
  109.             {  
  110.             }  
  111.   
  112.             @Override  
  113.             public void mouseEntered(MouseEvent e)  
  114.             {  
  115.             }  
  116.   
  117.             @Override  
  118.             public void mouseClicked(MouseEvent e)  
  119.             {  
  120.             }  
  121.         });  
  122.   
  123.     }  
  124.   
  125. }  
  126.   
  127. package com.swing.menu;  
  128.   
  129. import java.awt.event.ActionEvent;  
  130. import java.awt.event.ActionListener;  
  131.   
  132. import javax.swing.JTextArea;  
  133. import javax.swing.JTextField;  
  134. import javax.swing.text.JTextComponent;  
  135.   
  136. import com.common.util.WindowUtil;  
  137.   
  138. /*** 
  139.  * 文本域的右键菜单响应事件. 
  140.  *  
  141.  * @author huangwei 
  142.  * 
  143.  */  
  144. public class Menu2ActionListener implements ActionListener {  
  145.     private JTextComponent area2;  
  146.   
  147.     public Menu2ActionListener(JTextComponent area2) {  
  148.         super();  
  149.         this.area2 = area2;  
  150.     }  
  151.   
  152.   
  153.     @Override  
  154.     public void actionPerformed(ActionEvent event) {  
  155.         String command = event.getActionCommand();  
  156.         if (command.equals(MenuUtil2.ACTION_STR_DELETE_CONTENT)) {  
  157.             // System.out.println("delete word");  
  158.             area2.replaceSelection("");  
  159.         } else if (command.equals(MenuUtil2.ACTION_STR_DELETE_ALL_CONTENT)) {  
  160.             area2.selectAll();  
  161.             area2.replaceSelection("");  
  162.         } else if (command.equals(MenuUtil2.ACTION_STR_SELECT_ALL_CONTENT)) {  
  163.             if (area2 != null) {  
  164.                 area2.selectAll();  
  165.                 // System.out.println("select all");  
  166.             }  
  167.         } else if (command.equals(MenuUtil2.ACTION_STR_COPY)) {  
  168.             String selectContent = area2.getSelectedText();  
  169.             if (selectContent == null || selectContent.equals("")) {  
  170.                 return;  
  171.             }  
  172.             WindowUtil.setSysClipboardText(selectContent);  
  173.         } else if (command.equals(MenuUtil2.ACTION_STR_COPY_ALL)) {  
  174.             String selectContent = area2.getText();  
  175.             if (selectContent == null || selectContent.equals("")) {  
  176.                 return;  
  177.             }  
  178.             WindowUtil.setSysClipboardText(selectContent);  
  179.         } else if (command.equals(MenuUtil2.ACTION_STR_PASTE)) {  
  180.             String content = WindowUtil.getSysClipboardText();  
  181.             if (content == null || content.equals("")) {  
  182.                 return;  
  183.             }  
  184.             int caret = area2.getCaretPosition();  
  185.             if(area2 instanceof JTextArea){  
  186.                 JTextArea ta2=(JTextArea)area2;  
  187.                 ta2.insert(content, caret);  
  188.             }  
  189.         } else if (command.equals(MenuUtil2.ACTION_STR_REPLACE_ALL)) {  
  190.             String content = WindowUtil.getSysClipboardText();  
  191.             if (content == null || content.equals("")) {  
  192.                 return;  
  193.             }  
  194.             area2.setText(content);  
  195.         }  
  196.   
  197.     }  
  198.   
  199. }  
相关文章
|
5月前
|
存储 Python
Tkinter: 选项按钮与复选框
Tkinter: 选项按钮与复选框
|
Java
swing做一个简单的记事本(有菜单的样式、右键弹出菜单、以及实现“新建”和“打开”功能)
swing做一个简单的记事本(有菜单的样式、右键弹出菜单、以及实现“新建”和“打开”功能)
332 0
swing做一个简单的记事本(有菜单的样式、右键弹出菜单、以及实现“新建”和“打开”功能)
IDEA 隐藏编辑器顶部 Tab 栏
版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82711391 ...
1686 0
|
区块链 Python 数据格式