如何在Java中创建隐藏菜单项的快捷方式 [英] How to make shortcut to invisible menu item in Java
问题描述
我最近在我的程序中添加了一个控制台对话框,以便更容易地捕获bug。目前可以通过JMenuItem访问它,并且键盘快捷键为Ctrl + L。
I've recently added a console dialog to my program to make catching bugs easier. This is currently accessible through a JMenuItem and has keyboard shortcut 'Ctrl+L'.
理想情况下我想保留此选项但是使菜单项隐藏/隐藏,因为我真的希望人们使用它,如果他们将内容复制给我,所以我可以调查一些事情。但是,当我将菜单项设置为不可见时,快捷方式也会停止工作。
Ideally I'd like to keep this option in but make the menu item hidden/invisible, because I only really want people to use it if they are copying the contents to me so I can investigate something. However, when I set the menu item to be invisible the shortcut also stops working.
这是我尝试过的:
mntmViewLogs.setAccelerator(KeyStroke.getKeyStroke('L', KeyEvent.CTRL_DOWN_MASK));
mntmViewLogs.setVisible(false);
谢谢!
推荐答案
您应该使用密钥绑定API: http:/ /docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
You should use the key bindings API: http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
将加速键不设置为菜单,而是设置为后面的操作menu:
getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
acceleratorKey,
actionname);
Set accelerator key not to menu, but to the action behind the menu: getInputMap(WHEN_IN_FOCUSED_WINDOW).put( acceleratorKey, actionname);
这篇关于如何在Java中创建隐藏菜单项的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!