gpt4 book ai didi

Java-KeyEvent 方法不起作用

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

我正在制作一种乒乓球游戏,但我有一个问题。我的代码中有一个方法,用于检查用户输入是否按下了某个键。但当我按下(在我的例子中)UP键时它不会执行。

这是代码,抱歉英语不好,请帮助我:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GamePanel extends JPanel implements ActionListener,KeyListener{
Player player = new Player();
Ball ball = new Ball();

public GamePanel(){
Timer time = new Timer(50, this);
time.start();
}

private void update(){
player.update();
ball.update();
}

public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 600);
player.paint(g);
ball.paint(g);
}
public void actionPerformed(ActionEvent e){
update();
repaint();
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP)
{
player.setyv(-5);
}
}
public void keyReleased(KeyEvent e){

}
public void keyTyped(KeyEvent e){

}
}

最佳答案

再说一次,

  1. 使用按键绑定(bind)而不是 KeyListener,因为这可以帮助您将焦点从图片中移开,而无需使用拼凑。
  2. 始终确保在覆盖中调用 super 的paintComponent方法。

例如:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GamePanel extends JPanel implements ActionListener {
private static final int PREF_W = 800;
private static final int PREF_H = 600;
Player player = new Player();
Ball ball = new Ball();

public GamePanel() {
Timer time = new Timer(50, this);
time.start();

// !! set key bindings
int condition = WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();

KeyStroke up = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
inputMap.put(up, up.toString());
actionMap.put(up.toString(), new AbstractAction() {

@Override
public void actionPerformed(ActionEvent evt) {
player.setyv(-5);
}
});

KeyStroke down = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
inputMap.put(down, down.toString());
actionMap.put(down.toString(), new AbstractAction() {

@Override
public void actionPerformed(ActionEvent evt) {
player.setyv(5);
}
});
}

private void update() {
player.update();
ball.update();
}

// !! public void paintComponent(Graphics g) {
protected void paintComponent(Graphics g) {
super.paintComponent(g); // !!
g.setColor(Color.BLACK);
g.fillRect(0, 0, PREF_W, PREF_H); // !!
player.paint(g);
ball.paint(g);
}

// !!
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

public void actionPerformed(ActionEvent e) {
update();
repaint();
}

// !!
private static void createAndShowGui() {
GamePanel mainPanel = new GamePanel();

JFrame frame = new JFrame("GamePanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

interface Playable {
void update();

void paint(Graphics g);
}

class Player implements Playable {
private static final Color PLAYER_COLOR = Color.RED;
private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
private int x = 400;
private int y = 400;
private int yv = 0;
private int xv = 0;

@Override
public void update() {
y += yv;
x += xv;
}

public void setyv(int i) {
yv += i;
}

@Override
public void paint(Graphics g) {
g.setFont(FONT);
g.setColor(PLAYER_COLOR);
g.drawString("P", x, y);
}

}

class Ball implements Playable {

@Override
public void update() {
// TODO Auto-generated method stub

}

@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub

}

}

关于Java-KeyEvent 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158662/

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