简体   繁体   English

需要在swing applet中禁用F10键的Frame Focus

[英]Need to disable F10 key's Frame Focus in swing applet

我使用F10作为更新数据进入数据库的快捷方式。每当我按F10更新工作正常但我的焦点移动到菜单栏(该栏包含关闭,最小化,最大化)是否还有停止这个?我知道这是Windows快捷方式那有可能阻止它吗?

You may try to remove the WHEN_IN_FOCUSED_WINDOW action from your JMenuBar . 您可以尝试从JMenuBar删除WHEN_IN_FOCUSED_WINDOW操作。 Just add this line into your code : 只需将此行添加到您的代码中:

menuBar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F10"), "none");

Where the menuBar is the reference for your JMenuBar component. menuBarJMenuBar组件的引用。

Hope this helps. 希望这可以帮助。

EDIT : 编辑:

Indeed, in case you're not using JMenuBar the above solution does not work. 实际上,如果您没有使用JMenuBar ,上述解决方案无效。 An alternative solution is to create an empty action and use it to bind the F10 key. 另一种解决方案是创建一个空操作并使用它来绑定F10键。 (see Key Bindings ). (参见Key Bindings )。

Here is an example : 这是一个例子:

 //create an empty action which do nothing
Action emptyAction = new AbstractAction(){
     public void actionPerformed(ActionEvent e)
     {   //do nothing here   }};

//bind F10 with the empty action
KeyStroke f10 = KeyStroke.getKeyStroke( "F10");
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(f10, "F10");
frame.getRootPane().getActionMap().put("F10", emptyAction);

Where frame is your JFrame component. frame是您的JFrame组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM