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

  • Home
  • SEARCH
  • 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 7071649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:48+00:00 2026-05-28T05:42:48+00:00

When I try hitting the Space Bar nothing happens. I tried this with different

  • 0

When I try hitting the Space Bar nothing happens. I tried this with different keys but nothing works. Could someone please tell me what I am doing wrong? or why this isnt working?

Also I am using Java SE 1.6 to compile this.

Here is my Code:

package bigbass1997;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class Main extends JFrame implements KeyListener, MouseListener, MouseMotionListener{

    // VARIABLES
    public static String title = "Royal Casino";
    public static String author = "bigbass1997";
    public static String version = "0.0.0";

    GamePanel gp;

    public Main(){
        gp = new GamePanel();
        this.setSize(GamePanel.gameDim);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setTitle(title + " " + version);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.addKeyListener(this);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.add(gp);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_SPACE){
            Slots.slotsThread.start();
            System.out.println("Slot THREAD Started"); 
            GamePanel.slotsplaying = true;
        }
    }

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        Main m = new Main();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }


    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}
  • 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-05-28T05:42:49+00:00Added an answer on May 28, 2026 at 5:42 am

    It’s a focus issue — KeyListeners only work if the component that is being listened to has the focus. Use Key Bindings instead. There’s a decent Key Bindings tutorial on the subject at the Java Swing tutorials.

    For example:

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class BigBassMain extends JFrame {
    
        private static final String SPACE_BAR = "space bar";
       // VARIABLES
        public static String title = "Royal Casino";
        public static String author = "bigbass1997";
        public static String version = "0.0.0";
    
        GamePanel gp;
    
        public BigBassMain(){
            gp = new GamePanel();
            this.setSize(GamePanel.gameDim);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            this.setTitle(title + " " + version);
            this.setResizable(false);
            this.setLocationRelativeTo(null);
            this.add(gp);
    
            ActionMap actionMap = gp.getActionMap();
            InputMap inputMap = gp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), SPACE_BAR);
            actionMap.put(SPACE_BAR, new AbstractAction() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                Slots.slotsThread.start();
                System.out.println("Slot THREAD Started"); 
                GamePanel.slotsplaying = true;
             }
          });
        }
    
        public static void main(String[] args) {
            @SuppressWarnings("unused")
            BigBassMain m = new BigBassMain();
        }
    
    }
    

    Also, you really don’t want to have your GUI classes implement your listeners as it’s asking one poor little class to do too much. Instead either use anonymous inner classes for your listeners or separate classes.

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

Sidebar

Related Questions

i've tried using google maps marker manager, but i seem to be hitting a
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
I have a JSON request, but it seems that it is not hitting the
Hi guys just wondering if somebody could help me try and correctly thread my
Ok, let's try to get this straight: my final intent is to provide a
I had some JavaScript code which was working but when I tried to put
Currently I'm hitting a hard limit of 130688 bytes. If I try and send
I try to understand Firefox's behavior regarding the added prevent this page from creating
I've written this to try and log onto a forum (phpBB3). import urllib2, re

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.