Lucca Dornelles
Lucca Dornelles

Reputation: 31

key listener not working for some reason

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);
        }
    }
}

and this is the main class:

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

Upvotes: 2

Views: 121

Answers (1)

knoppiks
knoppiks

Reputation: 48

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

Changing

p.addKeyListener(new KeyListener()

to

this.addKeyListener(new KeyListener()

works for me.

Upvotes: 2

Related Questions