简体   繁体   English

Java:JPanel没有拿起键盘绑定

[英]Java: JPanel doesn't pick up keyboard bindings

Problem 问题

I've been fiddling to make keybindings work properly in an application I've written. 我一直在努力使键绑定在我编写的应用程序中正常工作。

Previously, I've been using a variant of the following; 以前,我一直在使用以下变体; panel.registerKeyboardAction(this, "createNewFood", KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);

But since I read in the documentation that registerKeyboardAction was marked as deprecated, I tried switching to the preferred method, which goes something like this; 但是,由于我在文档中读到了registerKeyboardAction被标记为已弃用,我尝试切换到首选方法,就像这样; panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), new NewFoodAction());

Unfortunately this doesn't seem to be working. 不幸的是,这似乎不起作用。

What I've Tried 我试过的

I've searched the web and I've tried a bunch of different approaches unsuccessfully; 我搜索过网络,我尝试了一系列不同的方法,但没有成功;

  • Instead of binding the key to the panel I tried attaching it to the result of getRootPane() . 我没有将键绑定到面板,而是尝试将其附加到getRootPane()的结果中。 Didn't work. 没工作。
  • I've tried all of the different "conditions"; 我尝试了所有不同的“条件”; WHEN_IN_FOCUSED_WINDOW , WHEN_ANCESTOR_OF_FOCUSED_COMPONENT , WHEN_FOCUSED , didn't work. WHEN_IN_FOCUSED_WINDOWWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSED ,无效。
  • I tried setting panel.setFocusable(true) ; 我试过设置panel.setFocusable(true) ; didn't work. 没用。
  • I tried using panel.requestFocusInWindow() just to see if it could work conditionally; 我尝试使用panel.requestFocusInWindow()来查看它是否可以有条件地工作; didn't work. 没用。

If I attach the keybinding to another component, for instance a JTextField, then it works as it's supposed to. 如果我将键绑定附加到另一个组件,例如JTextField,那么它可以正常工作。

Some other information that might be relevant (but I don't really think it is); 其他一些可能相关的信息 (但我并不认为是这样);

  • I'm using MigLayout for the panel. 我正在使用MigLayout作为面板。 Don't think this affects anything but who knows. 不要认为这会影响任何事情,但谁知道。
  • I have other keybindings present (that is, other keystrokes bound to other components) 我有其他键绑定(也就是说,其他键击绑定到其他组件)

Here's some sample code: 这是一些示例代码:

public FoodFrame() {
    super("MealTrack");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(1400, 600));
    setLocation(300, 100);
    setVisible(true);

    panel = new JPanel(new MigLayout("fill", "[grow][]", "[][][][grow][][]"));
    add(panel);
  panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), new NewFoodAction());

    pack();
    filter.requestFocusInWindow();
}

private class NewFoodAction extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("called");
    }

}

} }

Does anyone know what the problem seems to be? 有谁知道这个问题似乎是什么?

According to the Jcomponent documentation , you're mapping the key inputs, but the action they perform isn't actually mapped to the panel. 根据Jcomponent文档 ,您正在映射键输入,但它们执行的操作实际上并未映射到面板。 for the code... panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), "newfood!"); 代码... panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control I"), "newfood!");

... you must also have ... ......你还必须......

panel.getActionMap().put("newfood!", [Some actionListener that does what you need to do]);

Not entirely sure that will correct the issue, but hopefully that fixes it for you. 不完全确定会纠正这个问题,但希望能为您解决问题。 Good luck! 祝好运!

You are doing it wrong. 你做错了。 You need to use both ActionMap and InputMap . 您需要同时使用ActionMap和InputMap You should do: 你应该做:

panel.getInputMap(con).put(KeyStroke.getKeyStroke("control I"), "createNewFood");
panel.getActionMap().put("createNewFood", new NewFoodAction());

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

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