Consultez la section How To Use Key Bindings du tutoriel Java.
Vous devez créer et enregistrer un Action
avec ActionMap
de votre composant et enregistrer un (KeyStroke
, Nom de l'action) paire dans l'un des InputMap
s de la composante de votre application. Étant donné que vous n'avez pas de JMenuBar
, vous pouvez simplement enregistrer les raccourcis clavier avec un JPanel
de niveau supérieur dans votre application.
Par exemple:
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");
ça marche! Merci. – n002213f
Pas de problème - Vous pouvez accepter ma solution si vous le souhaitez! (J'ai besoin des points!). – Adamski