如何给文本框增加快捷键呢?
Ctrl+Z :恢复(undo)
Ctrl+C :拷贝
Ctrl+X :剪切
Ctrl+V :黏贴
Ctrl+Y :重做
Ctrl+R :使...只读
Ctrl+E :使...可编辑
Ctrl+D :清空文本框
文本框快捷键
- public void addActionMap() {
- getActionMap().put("Undo", new AbstractAction("Undo11") {
- private static final long serialVersionUID = 2434402629308759912L;
- public void actionPerformed(ActionEvent evt) {
- try {
- boolean b = undo.canUndo();
- // System.out.println("whether undo : "+b);
- if (b) {
- undo.undo();
- }
- } catch (CannotUndoException e) {
- }
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");
- getActionMap().put("Redo", new AbstractAction("Redo1111") {
- private static final long serialVersionUID = 5348330289578410517L;
- public void actionPerformed(ActionEvent evt) {
- try {
- if (undo.canRedo()) {
- undo.redo();
- }
- } catch (CannotRedoException e) {
- }
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control R"), "Redo");
- getActionMap().put("Copy", new AbstractAction("Copy111") {
- private static final long serialVersionUID = -5151480809625853288L;
- public void actionPerformed(ActionEvent evt) {
- copy();
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control C"), "Copy");
- getActionMap().put("Cut", new AbstractAction("Cut") {
- private static final long serialVersionUID = 7316612864835857713L;
- public void actionPerformed(ActionEvent evt) {
- cut();
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control X"), "Cut");
- getActionMap().put("Paste", new AbstractAction("Paste111") {
- private static final long serialVersionUID = -3548620001691220571L;
- public void actionPerformed(ActionEvent evt) {
- paste();
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control V"), "Paste");
- // redo Ctrl + Y
- getActionMap().put("Redo", new AbstractAction("reDo111") {
- private static final long serialVersionUID = -3548620001691220571L;
- public void actionPerformed(ActionEvent evt) {
- if (undo.canRedo()) {
- undo.redo();
- }
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
- getActionMap().put("Save", new AbstractAction("save111") {
- private static final long serialVersionUID = -3548620001691220571L;
- public void actionPerformed(ActionEvent evt) {
- dealSave((JTextComponent)evt.getSource());
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control S"), "Save");
- //按Ctrl+R 使文本框只读,不可编辑
- getActionMap().put("Readonly", new AbstractAction("Readonly111") {
- private static final long serialVersionUID = -3548620001691220571L;
- public void actionPerformed(ActionEvent evt) {
- JTextComponent tf=(JTextComponent)evt.getSource();
- if(!ValueWidget.isNullOrEmpty(tf)){
- tf.setEditable(false);
- }
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control R"), "Readonly");
- //按Ctrl+E 使文本框可编辑
- getActionMap().put("Editable", new AbstractAction("Editable111") {
- private static final long serialVersionUID = -3548620001691220571L;
- public void actionPerformed(ActionEvent evt) {
- JTextComponent tf=(JTextComponent)evt.getSource();
- if(!ValueWidget.isNullOrEmpty(tf)){
- tf.setEditable(true);
- tf.requestFocus();
- // tf.repaint();
- tf.updateUI();
- }
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control E"), "Editable");
- //按Ctrl+D 清空文本框
- getActionMap().put("cleanUp", new AbstractAction("cleanUp111") {
- private static final long serialVersionUID = -3548620001691220571L;
- public void actionPerformed(ActionEvent evt) {
- JTextComponent tf=(JTextComponent)evt.getSource();
- if(!ValueWidget.isNullOrEmpty(tf)){
- tf.setText(SystemHWUtil.EMPTY);
- tf.requestFocus();
- }
- }
- });
- getInputMap().put(KeyStroke.getKeyStroke("control D"), "cleanUp");
- }
具体见源代码中的com.swing.component.UndoTextArea