1

我正在尝试在我的代码中使用 KeyListener ......但它不起作用,我认为 KeyListener 没有响应......

如果你们看到有什么不对的地方请告诉我。我不知道为什么它不起作用。提前致谢。

这是代码。

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

我的面板类

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

}
4

3 回答 3

2

您应该将 放在this.addKeyListener(this);MyPanel 类构造函数中,而不是 paintComponent 方法中。

于 2012-09-13T16:40:54.737 回答
2

addKeyListener您应该在类中注释掉MyPanel并在实例化后在 Main 类中执行此操作MyPanel

frame.addKeyListener(panel);
于 2012-09-13T16:47:15.880 回答
1

请始终将 aKeyBindings用于此类任务。请参阅:http ://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

作为一个小提示,我建议您将其实现KeyListenerAnonymousClass.

于 2012-09-13T16:42:58.567 回答