简体   繁体   中英

java - Why wont my KeyListener do anything?

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

    }

}

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.

You're adding the KeyListener to the wrong JComponent. Try this:

this.getGlassPane().addKeyListener(this);

More information about how the Swing library works concerning this issue can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html

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