Consulte la sección How To Use Key Bindings del tutorial de Java.
lo necesario para crear y registrar una Action
con ActionMap
de su componente y el registro de un par (KeyStroke
, Nombre de acción) en una de InputMap
s de los componentes de la aplicación. Dado que no tiene un JMenuBar
, simplemente puede registrar los enlaces de teclas con un nivel superior JPanel
en su aplicación.
Por ejemplo:
Action action = new AbstractAction("Do It") { ... };
// This is the component we will register the keyboard shortcut with.
JPanel pnl = new JPanel();
// Create KeyStroke that will be used to invoke the action.
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK);
// Register Action in component's ActionMap.
pnl.getActionMap().put("Do It", action);
// Now register KeyStroke used to fire the action. I am registering this with the
// InputMap used when the component's parent window has focus.
pnl.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "Do It");
funciona! Gracias. – n002213f
No hay problema - ¡Puedes aceptar mi solución si quieres! (¡Necesito los puntos!). – Adamski