Java Swing 快捷键
给Java Swing 编程中按钮或者其他组件事件添加快捷键的方法:
Component.setAccelerator(KeyStroke.getKeyStroke(‘Q’, InputEvent.CTRL_MASK));
这个快捷键是ctrl+Q,通过这个方法即可实现点击操作与ctrl+Q快捷键操作同样的效果
为JButton设置ctrl快捷键
this.jButton_save.registerKeyboardAction(new SaveListener(), KeyStroke.getKeyStroke( KeyEvent.VK_S, KeyEvent.CTRL_MASK),JComponent.WHEN_IN_FOCUSED_WINDOW);
为JMenuItem添加快捷键
openJMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
mnuFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
快捷键:alt + 1
给菜单加上助记符 mnuFile.setMnemonic(‘F’);