java – 按“ctrl”“alt”“backspace”后执行某项任务

当我想要执行某些操作时,如何编写代码.假设当用户同时按下ctrl alt退格键时,将弹出对JTextField和消息框的关注.

解决方法:

密钥绑定的示例:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;

public class JTextFieldMagic {
   public static final String CTRL_ALT_BACK_SPACE = "ctrlAltBackspace";

   public static void main(String[] args) {
      JTextField field = new JTextField(10);

      int condition = JComponent.WHEN_FOCUSED;
      InputMap inputmap = field.getInputMap(condition);
      ActionMap actionMap = field.getActionMap();

      KeyStroke ctrlAltBackSpaceKeyStroke = KeyStroke.getKeyStroke(
            KeyEvent.VK_BACK_SPACE, 
            KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK);

      inputmap.put(ctrlAltBackSpaceKeyStroke, CTRL_ALT_BACK_SPACE);
      actionMap.put(CTRL_ALT_BACK_SPACE, new CtrlAltBackspaceAction());

      JOptionPane.showMessageDialog(null, field);
   }
}

class CtrlAltBackspaceAction extends AbstractAction {
   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println(JTextFieldMagic.CTRL_ALT_BACK_SPACE);
   }
}
上一篇:Linux下将UTF8编码批量转换成GB2312编码的方法


下一篇:javascript – 如何在jQuery Mobile中等待弹出窗口关闭?