简体   繁体   English

MouseListener / KeyListener不起作用(JPanel)

[英]MouseListener/KeyListener not working (JPanel)

I'm doing a little project that involves the mouse and key listeners in JPanel. 我正在做一个小项目,涉及JPanel中的鼠标和键监听器。 Unfortunately, none of the methods are called when I use the mouse/keyboard. 不幸的是,当我使用鼠标/键盘时,没有一个方法被调用。 I have worked with JPanels/JFrame/JApplet and JComponents before. 我之前曾使用过JPanels / JFrame / JApplet和JComponents。 The code snippets are shown below: 代码片段如下所示:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Scanner;

public class Hello extends JPanel implements KeyListener, MouseListener{
    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    public Hello(){
        addKeyListener(this);
        addMouseListener(this);
    }
    public static void main(String [] args){
        Hello play = new Hello();
        play.setPanel();
    }
    public void setPanel(){
        panel.setLayout(null);
        frame.add(panel);
        frame.setLayout(null);
        panel.setBounds(0,0,100,100);
        frame.setVisible(true);
        panel.setVisible(true);
        panel.setFocusable(true);
        frame.setSize(100,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void keyTyped(KeyEvent evt){
        System.out.println("keytyped");
    }
    public void keyPressed(KeyEvent evt){
        System.out.print("keypressed");
    }
    public void keyReleased(KeyEvent evt){
        System.out.println("keyreleased");
    }
    public void mousePressed(MouseEvent evt){
        System.out.println("mousepressed");
    }
    public void mouseReleased(MouseEvent evt){
        System.out.println("mousereleased");
    }
    public void mouseClicked(MouseEvent evt){
        System.out.println("mouseclicked");
    }
    public void mouseEntered(MouseEvent evt){
        System.out.println("mousenentered");
    }
    public void mouseExited(MouseEvent evt){
        System.out.println("mouseexited");
    }
}

Off topic: I keep getting the error Your post appears to contain code that is not properly formatted as code. 主题外:我一直收到错误消息您的帖子似乎包含未正确格式化为代码的代码。 Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. 请使用代码工具栏按钮或CTRL + K键盘快捷键将所有代码缩进4个空格。 For more editing help, click the [?] toolbar icon. 要获得更多的编辑帮助,请单击[?]工具栏图标。 I have no idea how to fix it. 我不知道如何解决它。 Sometimes I put everything in code and it still won't submit. 有时我将所有内容都放入代码中,但仍然无法提交。

Have a look at Java KeyListener for JFrame is being unresponsive? 看看Java KeyListener for JFrame没有响应吗? .

You need to register your KeyListener and MouseListener for every JComponent you want to listen to: 您需要为每个要侦听的JComponent注册KeyListenerMouseListener

public Hello() {
    addKeyListener(this);
    addMouseListener(this);
    panel.addKeyListener(this);
    panel.addMouseListener(this);
    frame.addKeyListener(this);
    frame.addMouseListener(this);
}

Edit: 编辑:
Key and mouse events are only fired from the JComponent which has focus at the time. 仅从当时具有焦点的JComponent触发键和鼠标事件。 Because of this there seems to be a consensus that KeyBindings may be favorable to KeyListeners. 因此,似乎已经达成共识,即KeyBindings可能对KeyListeners有利。 The two have their applications, however, and so there is no hard and fast rule here. 但是,两者都有其应用,因此这里没有硬性规定。 Have a read of ' How to Write a Key Listener ' and ' How to Write a Key Binding ' and you'll get the gist. 阅读“ 如何编写密钥侦听器 ”和“ 如何编写密钥绑定 ”,您将了解要点。

Better to avoid using KeyListeners with JPanel , use KeyBindings instead. 最好避免将KeyListenersJPanel使用,而应使用KeyBindings JPanel cannot gain focus so cannot interact with KeyEvents . JPanel无法获得焦点,因此无法与KeyEvents交互。 Using KeyBindings , you can map an Action to a KeyStroke even when a component doesn't have focus. 使用KeyBindings ,即使组件没有焦点,您也可以将Action映射到KeyStroke。

Try this instead: 尝试以下方法:

 panel.addKeyListener(this);
 panel.addMouseListener(this);

You have to add the listeners to every component you want to listen to. 您必须将侦听器添加到要侦听的每个组件中。

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

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