Java KeyBindings:它是如何工作的? [英] Java KeyBindings: How does it work?

查看:209
本文介绍了Java KeyBindings:它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到我应该使用键绑定而不是KeyListeners.因此,我找到了以下页面:键绑定.我仔细阅读并尝试实现它.

    Action numPressed = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("hi");
        }
    };
    this.getInputMap().put(KeyStroke.getKeyStroke("1"), "released");
    this.getActionMap().put("released", numPressed);

我决定只是看看会发生什么.这个类扩展了JPanel.之前,我使用了this.setFocusable(true).但是,当我键入"1"时,我什么也看不见.我究竟做错了什么?应该如何实现键绑定?

解决方案

您的示例仅在面板具有焦点时才有效,请尝试使用getInputMap(WHEN_IN_FOCUSED_WINDOW).

您可能还会发现您的KeyStroke语句也不起作用,我建议尝试使用KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true)代替,这将为您提供一个可以响应键释放的KeyStroke对象.

已更新示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestKeyBindings04 {

    public static void main(String[] args) {
        new TestKeyBindings04();
    }

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "pressed");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "released");

            am.put("pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Pressed");
                }
            });

            am.put("released", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("released");
                }
            });

            setFocusable(true);
            requestFocusInWindow();        
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }
    }
}

I keep seeing that I should use Key Bindings instead of KeyListeners. So I found this page: Key Bindings. I read through it and tried to implement it.

    Action numPressed = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("hi");
        }
    };
    this.getInputMap().put(KeyStroke.getKeyStroke("1"), "released");
    this.getActionMap().put("released", numPressed);

I decided to just see what would happen. The class this is in extends a JPanel. Earlier, I used this.setFocusable(true). However, I don't see anything happen when I type '1'. What am I doing wrong? How are Key Bindings supposed to be implemented?

解决方案

Your example will only work IF the panel has focus, try using getInputMap(WHEN_IN_FOCUSED_WINDOW) instead.

You may also find that your KeyStroke statement won't work either, I'd suggest trying to use KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true) instead, which will provide you with a KeyStroke object that will respond to key releases.

Updated with example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestKeyBindings04 {

    public static void main(String[] args) {
        new TestKeyBindings04();
    }

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, false), "pressed");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0, true), "released");

            am.put("pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Pressed");
                }
            });

            am.put("released", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("released");
                }
            });

            setFocusable(true);
            requestFocusInWindow();        
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }
    }
}

这篇关于Java KeyBindings:它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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