[英]How to add a shortcut (Mnemonics) to a JMenuItem
I have an 'about' JMenuItem that when clicked displays a text box of what the application does. 我有一个“关于” JMenuItem,单击该菜单时将显示一个文本框,显示应用程序的功能。 I'd like the user to be able to access this via a shortcut (mnemonic).
我希望用户能够通过快捷方式(助记符)访问它。 This is my current code.
这是我当前的代码。 (I have already tried numerous times I just thought it would be better to post the code with no errors).
(我已经尝试了无数次,我只是认为最好发布无错误的代码)。
JMenuItem item2 = new JMenuItem("About", new ImageIcon("aboutIcon.gif"));
m1.add(item2);
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "This application converts numerous values"
+ " of different formats"
+ " inputted by the user, and displays the desired "
+ "result.\n Author: James Cordiner"
+ "\n© Copyright");
}
});
You are asking about an accelerator. 您在问加速器。 Use
采用
item2.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_A, ActionEvent.ALT_MASK) );
However, when using the ALT key you might run afoul of the mnemonic initiator of the current look-and-feel. 但是,使用ALT键时,您可能会违反当前外观的助记符启动器。 For example, if some main menu item has a mnemonic of 'A', hitting ALT+A will select that menu item rather than the one you want.
例如,如果某个主菜单项的助记符为“ A”,则按ALT + A将选择该菜单项,而不是您想要的菜单项。 It is probably better to use CTRL instead of ALT.
使用CTRL代替ALT可能更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.