java - 为什么我的KeyListener不做任何事情?

时间:2012-01-16 02:11:31

标签: java swing jpanel key-bindings keylistener

当我尝试击中太空酒吧时没有任何反应。我尝试了不同的键,但没有任何作用。有人可以告诉我我做错了什么吗?或者为什么这不起作用?

我也在使用Java SE 1.6来编译它。

这是我的代码:

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

    }

}

2 个答案:

答案 0 :(得分:5)

这是一个焦点问题 - 只有正在收听的组件具有焦点时,KeyListeners才有效。请改用键绑定。在Java Swing教程中,这个主题有一个不错的Key Bindings tutorial

例如:

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

}

另外,你真的不希望你的GUI类实现你的听众,因为它要求一个可怜的小班做太多。而是为侦听器或单独的类使用匿名内部类。

答案 1 :(得分:-4)

您正在将KeyListener添加到错误的JComponent中。试试这个:

this.getGlassPane().addKeyListener(this);

有关Swing库如何解决此问题的更多信息,请访问:http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html