问题描述
我想为 JMenuItem 设置一个加速器.
I want to set an accelerator to a JMenuItem.
现在我是这样设置的
openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
它正在工作,但我不希望 ctrl+o 作为加速器.我想要空格"作为加速器,但我没有找到任何可以生成与空格"对应的 KeyStroke 的方法.
and it's working but I don't want ctrl+o as accelerator. I want 'space' as accelerator but I didn't find any method by which I can generate a KeyStroke corresponding to 'space'.
KeyStroke.getStroke()
要么接受一个字符要么(int,int).我也没有找到任何与空格对应的字符.
either takes a char or (int, int). I didn't find any char corresponding to space also.
推荐答案
大多数 UI 委托使用类似 METRICAL TETRASEME
的东西呈现 KeyEvent.VK_SPACE
加速器: ⏘ (U+23D8
).例如,Action
可能包括这些行:
Most UI delegates render a KeyEvent.VK_SPACE
accelerator using something like the METRICAL TETRASEME
: ⏘ (U+23D8
). For example, an Action
might include these lines:
static final int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, MASK));
这篇关于Java - 向 JMenuItem 添加加速器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!