java - 为什么我的 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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将 KeyListener 添加到了错误的 JComponent。尝试一下:
有关此问题的 Swing 库如何工作的更多信息可以在此处找到:http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html
You're adding the KeyListener to the wrong JComponent. Try 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
这是一个焦点问题——KeyListener 仅在正在侦听的组件具有焦点时才起作用。请改用按键绑定。 Java Swing 教程中有一个关于该主题的不错的按键绑定教程。
例如:
另外,你真的不想让你的 GUI 类实现你的监听器,因为它要求一个可怜的小类做太多事情。相反,要么为侦听器使用匿名内部类,要么使用单独的类。
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:
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.