gpt4 book ai didi

java - 为什么我的 KeyListener 什么都不做?

转载 作者:行者123 更新时间:2023-11-30 11:48:19 25 4
gpt4 key购买 nike

当我尝试按下空格键时,没有任何反应。我用不同的键试过了,但没有任何效果。有人可以告诉我我做错了什么吗?或者为什么这不起作用?

此外,我正在使用 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

}

}

最佳答案

这是一个焦点问题 -- KeyListeners 仅在被监听的组件具有焦点时才起作用。请改用键绑定(bind)。有一个体面的Key Bindings tutorial关于 Java Swing 教程中的主题。

例如:

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 类实现您的监听器,因为它要求一个可怜的小类做太多事情。相反,要么为您的监听器使用匿名内部类,要么使用单独的类。

关于java - 为什么我的 KeyListener 什么都不做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8875118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com