java - 使用按键绑定(bind)获取键码

标签 java swing mouseevent keycode key-events

我有一个显示缓冲图像的函数。我希望在加载图像的 JLabel 中实现鼠标监听器和按键监听器。由于 JLabel 无法获得焦点,我尝试使用 KeyBinding。但我注意到 KeyBinding 用于个人 key 。我想要的是每个按下的键的键码,无论按下哪个键。在这种情况下有办法获取 KeyCode 吗?我还注意到 KeyListener 和 MouseListener 在构造函数中工作,但在其他方法中不起作用。这是真的吗?

 public void imageloader(BufferedImage image) throws InterruptedException {
        // frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
        setSize(200, 100);
        setVisible(true);
        label.removeAll(); //label is Jlabel
        label.setIcon(new ImageIcon(image));
        frame.setSize(dimension);
        label.revalidate();
        JScrollPane pane = new JScrollPane(label,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        frame.getContentPane().add(pane);
        frame.setSize(dimension);
       // frame.setVisible(true);
    }

更新:

我改变了方法,尽管触发了MouseEvents,但仍然无法获得键盘焦点。

public void imageloader(BufferedImage image) throws InterruptedException {
 final String eol = System.getProperty("line.separator");
        final JTextArea ta = new JTextArea(15, 60);
        ta.setEditable(false);
        Dimension dimension = new Dimension(Bimage.getWidth(), Bimage.getHeight());
        gui.add(new JScrollPane(ta), BorderLayout.CENTER);

        gui.add(l, BorderLayout.NORTH);
        gui.setSize(dimension);
        l.setFocusable(true);
        //l.setOpaque(true);
        l.removeAll();
        l.setIcon(new ImageIcon(Bimage));
        l.revalidate();
        l.addFocusListener(new FocusListener() {
            Color focused = Color.CYAN;
            Color unfocused = Color.ORANGE;

            public void focusLost(FocusEvent fe) {
                System.out.println("Unfocused");
            }

            public void focusGained(FocusEvent fe) {
                System.out.println("Focused");

            }
        });

        l.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent me) {
                System.out.println("Mouse Entered");
            }

            @Override
            public void mouseExited(MouseEvent me) {
                System.out.println("Mouse Exited");

            }

            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse Clicked");
            }
        });
        l.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent ke) {
                System.out.println("Key Typed");
            }

            public void keyPressed(KeyEvent e) {
                System.out.println("Key Pressed");
            }
        });

        frame.getContentPane().add(gui);
        frame.setSize(dimension);
    }

最佳答案

I don't know from where you knew JLabel can get Focus. 
Documentation here clearly explains it cannot get Keyboard Focus.

让我们回到KeyBindings

myLabel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    KeyStroke.getKeyStroke((KeyEvent.DEL), 0, false), "DELETE");

   myLabel.getActionMap().put("DELETE", new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
          //       any/some Action
      }
   });

关于java - 使用按键绑定(bind)获取键码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7976209/

相关文章:

java - 如何将不同的回调传递给android中的同一个函数

c++ - QTableWidget 右键单击​​事件的问题

javascript - 根据鼠标位置更改区域的不透明度

java - 如何在 JDBC 中检索 MySQL 主键的最大值?

java - 内部服务器错误,正在以第二所有者身份上传 Google App Engine 项目

java - 在 Android textSwitcher 中使用上标

java - 添加横向 Jpanel

java - 为什么我的方法不能正确重绘?

java - 单选按钮不显示选定状态

javascript - 鼠标 ScrollLeft 不适用于 IE、Firefox、Safari