简体   繁体   English

在Java中是否有可能捕获箭头键,使其以相似的方式工作?Tab移动焦点?

[英]Is it possible in java to capture arrow keys to work in a simmilar way Tab move the focus?

I have a java program that includes a grid of buttons in a jpanel as input interface and a text box to show results. 我有一个Java程序,其中包括jpanel中的按钮网格作为输入界面和一个文本框来显示结果。 My goal is to be able to control the program with just arrow keys and space. 我的目标是仅使用箭头键和空格就能控制程序。

I know I can progam an event on every button to check if the left key was pressed while that button had the focus and make it pass the focus to the button on the left (and so on). 我知道我可以在每个按钮上编程一个事件,以检查当该按钮具有焦点时是否按下了左键,并将焦点传递给左按钮(依此类推)。

But I'd like to know if there is another way. 但我想知道是否还有另一种方法。 I like how Tab and shift+Tab work moving the focus to the previous or next focusable object, and i wonder if it could be done without specific code for every arrow_key-button combination. 我喜欢Tab和shift + Tab如何将焦点移到上一个或下一个可聚焦对象,并且我想知道是否可以在没有每种arrow_key-button组合特定代码的情况下完成此操作。

You cold write a method for all buttons that handles the arrow up. 您为所有向上处理箭头的按钮写了一个方法。 In this method you store the position of the current button in variables x and y . 在这种方法中,您将当前按钮的位置存储在变量xy

Now you loop through all controls on your form and check their positions. 现在,您遍历窗体上的所有控件并检查它们的位置。 Now you need to find one control with the same x but a lower y value (example for arrow up). 现在,您需要找到一个具有相同xy值较低的控件(例如向上箭头的示例)。

If you can´t find such a control, you reached the control of the form that is the "highest". 如果找不到这样的控件,则您达到了“最高”表单的控件。

Note: If your buttons aren´t in a straight rows and columns you need to look for controls that are near the value of you current control instead of exactly the same vlaue. 注意:如果您的按钮不是连续的行和列,则需要查找与您当前控件的值接近的控件,而不是完全相同的控件。

Hope I could help you. 希望我能为您服务。

I have a java program that includes a grid of buttons in a jpanel... My goal is to be able to control the program with just arrow keys and space. 我有一个Java程序,其中包含一个jpanel中的按钮网格...我的目标是仅使用箭头键和空格就能控制该程序。

You can use Key Bindings . 您可以使用Key Bindings Key bindings allow you to map an Action to a KeyStroke . 按键绑定使您可以将Action映射到KeyStroke

So you can create Key Bindings on the panel to handle the left/right arrow keys. 因此,您可以在面板上创建按键绑定以处理向左/向右箭头键。

First you need an Action: 首先,您需要一个操作:

class TabAction extends AbstractAction
{
    private boolean forward;

    public TabAction(boolean forward)
    {
        this.forward = forward;
    }

    public void actionPerformed(ActionEvent e)
    {
        if (forward)
            tabForward();
        else
            tabBackward();
    }

    private void tabForward()
    {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusNextComponent();
    }

    private void tabBackward()
    {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusPreviousComponent();
    }
}

Now you need to bind the Action to a KeyStroke on your panel: 现在,您需要将Action绑定到面板上的KeyStroke上:

InputMap im = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
KeyStroke tab = KeyStroke.getKeyStroke("TAB");
panel.getActionMap().put(im.get(tab), new TabAction(true));
KeyStroke shiftTab = KeyStroke.getKeyStroke("shift TAB");
im.put(shiftTab, shiftTab);
panel.textArea.getActionMap().put(im.get(shiftTab), new TabAction(false));

You would typically invoke the above code in the constructor of your panel containing the buttons, so "panel" would really be "this". 通常,您将在包含按钮的面板的构造函数中调用以上代码,因此“面板”实际上就是“ this”。

Read the section from the Swing tutorial on How to Use KeyBindings for more information. 阅读Swing教程中有关如何使用键绑定的部分, 获取更多信息。

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

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