问题描述:
若有一个按钮 button
如果设置快捷键alt+enter就可以设置成
button..setMnemonic(KeyEvent.VK_ENTER)
那么要设置快捷键ctrl+enter或单键enter 要怎样设置呢?
请高手指教~
解析:
button不能设置alt以外的快捷键,menuitem可以:
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_MASK))
//给按钮注册键盘事件当按下回车时相当于点击按钮
SymListener sy
jButton1.registerKeyboardAction(sy,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW)
//监听类
class SymListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource()
if (obj == jButton1) {
jButton1ActionPerformed(e)
} else if (obj == jButton2) {
jButton2ActionPerformed(e)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)