Java Jbutton KeyListener [英] Java Jbutton KeyListener

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

问题描述

我有一个标有1-9的3x3网格的jbuttons表示数字键盘。我添加了一个actionlistener和keylistener,它们都调用相同的函数,所以如果他们点击btn1或按数字键盘上的1就会发生同样的事情。

I have a 3x3 grid of jbuttons labeled 1-9 representing the number pad. I have added an actionlistener and keylistener which both call to the same function, so if they click btn1 or press 1 on the number pad the same thing happens.

问题是当我在小键盘上按1时,我想看到btn1按下它,如果这是有意义的。

The problem is when I press 1 on the numpad, I want to see btn1 press down with it, if that makes sense.

搜索没有引导我做任何事情,有没有名字?

Searching didn't lead me to anything, is there a name for that?

推荐答案

使用:


  • 键绑定API而不是 KeyListener 。密钥绑定唯一需要做的就是在相关按钮上调用 doClick ...

  • 使用 doClick 以编程方式点击按钮

  • The key bindings API instead of KeyListener. The only thing that the key binding needs to do, is call doClick on the associated button...
  • Use doClick to programmatically "click" the button

例如......

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author shane
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(3, 3));
            add(createButton("1", KeyEvent.VK_NUMPAD1));
            add(createButton("2", KeyEvent.VK_NUMPAD2));
            add(createButton("3", KeyEvent.VK_NUMPAD3));
            add(createButton("4", KeyEvent.VK_NUMPAD4));
            add(createButton("5", KeyEvent.VK_NUMPAD5));
            add(createButton("6", KeyEvent.VK_NUMPAD6));
            add(createButton("7", KeyEvent.VK_NUMPAD7));
            add(createButton("8", KeyEvent.VK_NUMPAD8));
            add(createButton("9", KeyEvent.VK_NUMPAD9));
        }

        protected JButton createButton(String name, int virtualKey) {
            JButton btn = new JButton(name);
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(e.getActionCommand() + " was clicked");
                }
            });
            btn.setMargin(new Insets(8, 8, 8, 8));
            InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = btn.getActionMap();
            im.put(KeyStroke.getKeyStroke(virtualKey, 0), "clickMe");
            am.put("clickMe", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton btn = (JButton) e.getSource();
                    btn.doClick();
                }
            });
            return btn;
        }

    }

}

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

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