Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:10:21+00:00 2026-06-11T11:10:21+00:00

I am trying to use KeyListener in my code…. But it’s not working, the

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:10:23+00:00Added an answer on June 11, 2026 at 11:10 am

    You should just comment out the addKeyListener in the MyPanel class and do this in the Main class after you instantiate the MyPanel:

    frame.addKeyListener(panel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use filehelpers class builder but I am kinda confused on what
I was trying use svn to find a checkin using svn log, but it
I am trying use std::copy to copy from two different iterator. But during course
I'm trying use the Sum method in a lambda expression for a comparison, but
I'm trying to have my alert dialog not only close itself when cancelled but
I am trying use TinyMCE to get code syntax to be displayed in the
I am trying use cursor for pagination. Forwarding pagination work fine, but I can
I am trying use Thread but i have some problem (I am beginner at
I'm trying use double type in openCL, but doesn't work anyway, i want use
I am trying use Accrev plugin for visual studio 2010. I am not sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.