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 7920375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:11:46+00:00 2026-06-03T16:11:46+00:00

I’m new to graphics programming and am having some difficluties with using a KeyListener

  • 0

I’m new to graphics programming and am having some difficluties with using a KeyListener to move and image left or right. Currently my code does not even register that a key is being pressed. If someone could help me out with just getting it to register this then I can do the rest myself.

Here is the frame code:

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameMain extends JFrame {

    final JPanel pnlShow;
    PanelHome pnlHome = new PanelHome();
    PanelPlayerInfo pnlPlayerInfo = new PanelPlayerInfo();
    PanelPlay pnlPlay = new PanelPlay(pnlPlayerInfo);
    PanelInstruction pnlInstructions = new PanelInstruction();
    PanelStore pnlStore = new PanelStore();
    PanelHighscores pnlHighscores = new PanelHighscores();
    ControlActionListenter CAL = new ControlActionListenter();

    public FrameMain() {
        pnlShow = new JPanel(new CardLayout());
        pnlShow.add(pnlHome, "Home");
        pnlShow.add(pnlPlay, "Play");
        pnlShow.add(pnlInstructions, "Instructions");
        pnlShow.add(pnlStore, "Store");
        pnlShow.add(pnlHighscores, "Highscores");
        pnlShow.add(pnlPlayerInfo, "PlayerInfo");

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("TANKS");
        this.setVisible(true);
        this.setSize(806, 628);
        this.setResizable(false);
        this.add(pnlShow);
        this.addKeyListener(new Move());

        pnlHome.btnExit.addActionListener(CAL);
        pnlHome.btnExit.setActionCommand("Exit");
        pnlHome.btnPlay.addActionListener(CAL);
        pnlHome.btnPlay.setActionCommand("PlayerInfo");
        pnlHome.btnInst.addActionListener(CAL);
        pnlHome.btnInst.setActionCommand("Instructions");
        pnlHome.btnHigh.addActionListener(CAL);
        pnlHome.btnHigh.setActionCommand("Highscores");
        pnlInstructions.btnBack.addActionListener(CAL);
        pnlInstructions.btnBack.setActionCommand("Main");
        pnlPlay.pnlToolbar.btnHome.addActionListener(CAL);
        pnlPlay.pnlToolbar.btnHome.setActionCommand("Main");
        pnlHighscores.btnBack.addActionListener(CAL);
        pnlHighscores.btnBack.setActionCommand("Main");
        pnlPlayerInfo.btnPlay.addActionListener(CAL);
        pnlPlayerInfo.btnPlay.setActionCommand("Play");
        pnlPlayerInfo.btnBack.addActionListener(CAL);
        pnlPlayerInfo.btnBack.setActionCommand("Main");

    }

    class ControlActionListenter implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            CardLayout cl = (CardLayout) (pnlShow.getLayout());
            String cmd = e.getActionCommand();

            if (cmd.equals("Main")) {
                cl.show(pnlShow, "Home");
            } else if (cmd.equals("Exit")) {
                System.exit(0);
            } else if (cmd.equals("Play")) {
                pnlPlay.arpPlayer[0].populateName(pnlPlayerInfo.txtPlayer1.getText());
                pnlPlay.arpPlayer[1].populateName(pnlPlayerInfo.txtPlayer2.getText());
                pnlPlay.pnlPlayer.lblPlayer1.setText(pnlPlay.arpPlayer[0].sPlayer);
                pnlPlay.pnlPlayer.lblPlayer2.setText(pnlPlay.arpPlayer[1].sPlayer);
                cl.show(pnlShow, "Play");
            } else if (cmd.equals("PlayerInfo")) {
                cl.show(pnlShow, "PlayerInfo");
            } else if (cmd.equals("Instructions")) {
                cl.show(pnlShow, "Instructions");
            } else if (cmd.equals("Highscores")) {
                cl.show(pnlShow, "Highscores");
            }
        }
    }

    class Move implements KeyListener {

        public void keyPressed(KeyEvent e) {
            System.out.println("rp");
        }

        public void keyTyped(KeyEvent e) {
            System.out.println("rp");
        }

        public void keyReleased(KeyEvent e) {
            System.out.println("rp");
        }
    }
}

I have added a keylistener to the frame and made a class that implements this keylistener. Like I said, all I want to do is have the program output something when I hit a key on the keyboard. If I need to show you anything else let me know and I will post it.

  • 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-03T16:11:49+00:00Added an answer on June 3, 2026 at 4:11 pm

    Try adding KeyListener to the components you need, not the whole JFrame. And make sure they are focused.

    Also you may find How to Use Key Bindings useful, as an alternative to key listeners.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.