简体   繁体   中英

KeyListener Java

I am trying to use KeyListener in my code.... But it's not working, the KeyListener not responding I think...

If you guys see anything wrong please tell me. I don't know why it's not working. Thanks in advance.

Here is the code.

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;

public class Main extends JFrame {
    static void drawFrame(JFrame frame) {

        frame.setSize(610, 805);
        frame.setLocation(145, 15);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("PacMan");
        drawFrame(frame);
        MyPanel panel = new MyPanel();
        panel.setBounds(00, 00, 610, 800);
        frame.setLayout(null);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().add(panel);
    }
}

MyPanel Class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyPanel extends JPanel implements KeyListener {

    private int xpac = 285, ypac = 570;

    public MyPanel() {
        this.requestFocus();
        this.requestFocusInWindow();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawMap1(g);
        drawPacman(g);
        addKeyListener(this);
    }

    void drawMap1(Graphics g) {

        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("pacmap1.png"));
        } catch (IOException e) {
            System.out.println("Can't find the Image.");
        }
        setBackground(Color.BLACK);
        g.drawImage(image, 0, 0, null);
    }

    void drawPacman(Graphics g) {

        int x = xpac, y = ypac;
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("pacright.png"));
        } catch (IOException e) {
            System.out.println("Can't find the Image.");
        }
        g.drawImage(image, x, y, null);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        System.out.println("Hi there Buddy");

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        System.out.println("Hi there Buddy");

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        System.out.println("Hi there Buddy");
    }

}

You should put the this.addKeyListener(this); in your MyPanel class constructor, not the paintComponent method.

你应该只注释掉addKeyListenerMyPanel类实例化后这样做的主要类MyPanel

frame.addKeyListener(panel);

Please always use a KeyBindings for such Tasks. See: http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

As a little tip, I would recommend you to implement KeyListener as an AnonymousClass .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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