简体   繁体   English

如何在窗口(显示窗口菜单)中禁用F10默认操作?

[英]How to disable F10 default action in window (show window menu)?

The default action of F10 is to show the menu of the window. F10的默认操作是显示窗口菜单。 However, I would like to disable this feature. 但是,我想禁用此功能。

UPDATED: Background: I would like to implement a special behavior in a JTextField if the user presses any key. 更新:背景:如果用户按下任何键,我想在JTextField中实现一种特殊的行为。 Unfortunately, the JTextField don't get the event when F10 is pressed because it is catched by the window (and the menu is shown). 不幸的是,按下F10时JTextField不会获得事件,因为它被窗口捕获(并且显示了菜单)。

Does anyone know how to disable this key binding in the window? 有谁知道如何在窗口中禁用此键绑定?

I tried to disable it in the root pane but without success: 我试图在根窗格中禁用它,但没有成功:

frame.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");

I searched a lot but found no solution for this issue. 我进行了很多搜索,但没有找到解决该问题的方法。 Maybe one of you knows an answer. 也许你们当中的一个知道答案。

UPDATE2 Here a code example to reproduce this behavior: UPDATE2这里是重现此行为的代码示例:

public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {

  @Override
  public void run() {
    JFrame.setDefaultLookAndFeelDecorated(true);

    final JTextField edit = new JTextField();
    edit.setEditable(false);
    edit.addKeyListener(new KeyAdapter() {

      @Override
      public void keyReleased(final KeyEvent ke) {
        edit.setText(KeyEvent.getKeyText(ke.getKeyCode()));
      }
    });

    final JFrame frame = new JFrame("DEMO");
    frame.setSize(320, 240);
    frame.getContentPane().add(edit);
    frame.setVisible(true);
  }
});

} }

Plase note: There is a different behavior according to whether "setDefaultLookAndFeelDecorated" is set to true or false. 请注意:根据“ setDefaultLookAndFeelDecorated”设置为true还是false,会有不同的行为。

Thanks in advance :) 提前致谢 :)

I tried to disable it in the root pane but without success: 我试图在根窗格中禁用它,但没有成功:

Check out Key Bindings for the bindings of all Swing components. 签出Key Bindings以获取所有Swing组件的绑定。

You will see that the F10 key is bound to the JMenuBar . 您将看到F10键绑定到JMenuBar So you should be able to use: 因此,您应该可以使用:

menuBar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");

Edit: 编辑:

Missed the point that you didn't have a menu bar. 错过了您没有菜单栏的问题。

It appears you can't just set the binding to "none". 看来您不能仅将绑定设置为“ none”。 Looks like Swing is still searching up the tree to find an Action to execute. 看起来Swing仍在搜索树以查找要执行的Action。 You need to provide a dummy Action that does nothing: 您需要提供一个不执行任何操作的虚拟Action:

Action action = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("do nothing");
    }
};

JPanel content = (JPanel)frame.getContentPane();
String key = "F10";
KeyStroke f10 = KeyStroke.getKeyStroke( key );
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(f10, key);
frame.getRootPane().getActionMap().put(key, action);

If I did undertand you correctly , Create Keypressed event for your JTextField on netbeans put to following code 如果我确实理解正确,则在netbeans上为JTextField创建Keypressed事件放入以下代码

private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       

   // Add below
       int key = evt.getKeyCode(); 

           if (evt.getSource() == jTextField1)
           {
               if (key == KeyEvent.VK_F10)
               {
                  // your actions here
                   System.out.println("Hello I am f10");

               }

           }
         //   end of if

        } 

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

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