Java KeyListener与Keybinding [英] Java KeyListener vs Keybinding

查看:121
本文介绍了Java KeyListener与Keybinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写计算器并遇到问题。我已经为所有按钮创建了一个actionlistener,现在我希望能够从键盘输入数据。我是否需要为KeyListener或Keybinding完成整个事情,还是有任何其他方法可以在单击按钮后将其发送到actionlistener中的指令?还有什么更好的:Keylistener或Keybinding

i am trying to write a calculator and having a problem. I already made a actionlistener for all buttons and now i want to make it possible to input data from keyboard. DO i need to do the whole thing for KeyListener or Keybinding or is there any other a way to make that after clicking a button it will be sent to the instructions in actionlistener? And whats better:Keylistener or Keybinding

推荐答案

一般来说,在你有一组有限的键输入的情况下,键绑定是更好的选择。

Generally speaking, where you have a limited set of key inputs, key bindings are a better choice.

KeyListener 遇到与可聚焦性相关的问题以及GUI中的其他控件,焦点将不断移动远离组件(使用 KeyListener )。

KeyListener suffers from issues related to focusability and with other controls in the GUI, focus will constantly be moving away from the component (with the KeyListener) all the time.

一个简单的解决方案是使用< a href =http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html =nofollow> 行动 s API 。这允许您定义一个自包含的action,它充当 ActionListener ,但也带有可用于配置其他UI组件的配置信息,特别是按钮

A simple solution would be to use the Actions API. This allows you to define a self contained "action" which acts as a ActionListener but also carries configuration information that can be used to configure other UI components, in particular, buttons

例如......

获取通用 NumberAction 可以代表任何数字(现在让我们将其限制为0-9)...

Take a generic NumberAction which could represent any number (lets limit it to 0-9 for now)...

public class NumberAction extends AbstractAction {

    private int number;

    public NumberAction(int number) {
        putValue(NAME, String.valueOf(number));
    }

    public int getNumber() {
        return number;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int value = getNumber();
        // Do something with the number...
    }

}

您可以执行以下操作...

You could do something like...

// Create the action...
NumberAction number1Action = new NumberAction(1);
// Create the button for number 1...
JButton number1Button = new JButton(number1Action);

InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
// Create a key mapping for number 1...
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "number1");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, 0), "number1");

ActionMap am = getActionMap();
// Make the input key to the action...
am.put("number1", number1Action);

你已经完成了......

And you're done...

您也可以为相同的数字创建 NumberAction 的任意数量的实例,这意味着您可以单独配置UI和绑定,但是知道在触发时,它们将会执行相同的代码逻辑,例如......

You can also create any number of instance of the NumberAction for the same number, meaning you could configure the UI and the bindings separately, but know that when triggered, they will execute the same code logic, for example...

这篇关于Java KeyListener与Keybinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆