Ознакомьтесь с разделом учебника по Java How To Use Key Bindings.
Вам нужно создать и зарегистрировать Action
с ActionMap
вашего компонента и зарегистрировать пару (KeyStroke
, Имя Действие) в одном из компонента вашего приложения InputMap
с. Учитывая, что у вас нет JMenuBar
, вы можете просто зарегистрировать привязки клавиш с помощью верхнего уровня JPanel
в своем приложении.
Например:
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");
это работает! Благодарю. – n002213f
Нет проблем - вы можете принять мое решение, если хотите! (Мне нужны очки!). – Adamski