4

当我想执行某事时如何编码。假设当用户同时按下ctrl++时,将弹出 JTextField 上的焦点alt和消息框backspace

4

2 回答 2

4

键绑定示例:

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);
   }
}
于 2012-10-06T13:57:24.017 回答
2

或者,如果您在“应用程序全局密钥侦听器”之后,您可以使用KeyboardFocusManager.addKeyEventDispatcheror Toolkit.addAWTEventListener

KeyboardFocusManager.addKeyEventDispatcher

public class GloablKeyListener {

    public static void main(String[] args) {
        new GloablKeyListener();
    }

    public GloablKeyListener() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });

    }

    protected class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {

            setLayout(new BorderLayout());
            label = new JLabel("Nothing happening here");
            label.setHorizontalAlignment(JLabel.CENTER);
            add(label);

            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

                @Override
                public boolean dispatchKeyEvent(KeyEvent e) {

                    if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && e.isControlDown() && e.isAltDown()) {                            
                        label.setText("Hit me");                        
                    } else {
                        label.setText("Nothing to see here...");
                    }

                    return false;
                }
            });    
        }
    }
}

Toolkit.addAWTEventListener

public class GloablKeyListener {

    public static void main(String[] args) {
        new GloablKeyListener();
    }

    public GloablKeyListener() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setLayout(new BorderLayout());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });

    }

    protected class TestPane extends JPanel {

        private JLabel label;

        public TestPane() {

            setLayout(new BorderLayout());
            label = new JLabel("Nothing happening here");
            label.setHorizontalAlignment(JLabel.CENTER);
            add(label);

            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                @Override
                public void eventDispatched(AWTEvent event) {
                    if (event instanceof KeyEvent) {
                        KeyEvent ke = (KeyEvent) event;
                        if (ke.getID() == KeyEvent.KEY_TYPED) {
                            if (ke.getKeyCode() == KeyEvent.VK_BACK_SPACE && ke.isControlDown() && ke.isAltDown()) {
                                label.setText("Hit me");
                            } else {
                                label.setText("Nothing to see here...");
                            }
                        }
                    }
                }
            }, AWTEvent.KEY_EVENT_MASK);

        }
    }
}

就个人而言,我更喜欢KeyboardFocusManager.addKeyEventDispatcher. 它更简单,更易于使用。

您可以设置自己的单例管理器,您可以在其中分配KeyStrokes 对Actions,就像键绑定一样。

于 2012-10-06T21:02:48.133 回答