关键侦听器由于某种原因无法正常工作 [英] key listener not working for some reason

查看:92
本文介绍了关键侦听器由于某种原因无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这段代码,当您启动它时,它会显示一个图像,然后在您按向左或向右键时在其他两个图像之间进行更改,但是由于某种原因,它没有从键盘读取输入,我尝试使用mouseListener并成功了,这是代码:

i made this code that when you start it it's suposed to show you an image and then change it between other two images when you press the left or right key, but for some reason it isn't reading the input from the keyboard, i tryed to use a mouseListener and it worked, this is the code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Implementary extends JFrame
{
    private static final long serialVersionUID = 1L;
    public Dimension d;
    public static ImageIcon Im = new ImageIcon(Implementary.class.getResource("death.png"));
    public static ImageIcon Imc = new ImageIcon(Implementary.class.getResource("right.png"));
    public static ImageIcon I = new ImageIcon(Implementary.class.getResource("left.png"));
    public static Image Img = Im.getImage();
    public static int x = 10;
    public static int y = 10;

    public Implementary()
    {
        super("hue");
        int x1 = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int y1 = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        d = new Dimension(x1, y1 - 45);
        this.setSize(d);
        this.setLocationRelativeTo(null);
        Panel p = new Panel();
        p.addKeyListener(new KeyListener()
        {
            @Override
            public void keyTyped(KeyEvent e)
            {
                keyPressed(e);
            }

            @Override
            public void keyPressed(KeyEvent e)
            {
                int k = e.getKeyCode();
                if (k == KeyEvent.VK_LEFT)
                {
                    Img = I.getImage();
                    repaint();
                    System.out.println(3);
                }
                else
                {
                    Img = Imc.getImage();
                    repaint();
                    System.out.println(2);
                }
                System.out.println(1);
            }

            @Override
            public void keyReleased(KeyEvent e)
            {
                keyPressed(e);
            }
        });
        this.add(p);
    }

    static class Panel extends JPanel
    {
        private static final long serialVersionUID = 1L;

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            this.setBackground(Color.cyan);
            g.drawImage(Img, x, y, null);
        }
    }
}

这是主要的类:

public class Yo
{
    public static void main(String args[])
    {
        Implementary imp = new Implementary();
        imp.setVisible(true);
    }
}

推荐答案

将KeyListener添加到整个JFrame可以解决问题. 由于您的JPanel无法选择/集中显示,因此不会收到击键.

Adding the KeyListener to the whole JFrame could do the trick. As your JPanel cannot be selected/focused it doesn't receive keystrokes.

更改

p.addKeyListener(new KeyListener()

this.addKeyListener(new KeyListener()

为我工作.

这篇关于关键侦听器由于某种原因无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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