gpt4 book ai didi

java - KeyPressed 在每个程序中都不起作用

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

我将一些程序从学校的计算机(Mac)传输到我的家用电脑。进入我的计算机后,我注意到按键现在在每个程序中都不起作用。我花了几个小时试图找出 KeyPressed 不起作用的原因。两台电脑都使用Eclipse

是因为Java版本不同还是操作系统不同?谢谢!

代码示例:

import java.awt.Graphics;
import java.awt.Image;
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 java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JApplet;

public class Skeleton extends JApplet implements Runnable, MouseMotionListener, MouseListener, KeyListener {

Random generator = new Random();
boolean GameRunning = true, PlayAgain = true;
int width, height;
int score = 0;
Image offscreenImage;

int XX;
int XY;
Image Cart;
Image candy[];
int candyX[];
int candyY[];
boolean up = false, down = false, left = false, right = false;
boolean candyRemaining[];
Graphics offscr;
Random r;
Thread t;
boolean inBounds = true;
boolean onScreen = true;
Image Title;
boolean hasStarted = false;

public Skeleton() {
r = new Random();
t = new Thread(this);
t.start();
}

public void init() {
XX = 0;
XY = 0;
candy = new Image[3];
candyRemaining = new boolean[3];
candyY = new int[3];
candyX = new int[3];
addKeyListener(this);
addMouseMotionListener(this);
addMouseListener(this);
setVisible(true);
setSize(500, 500);
width = getSize().width;
height = getSize().height;
Title = getImage(getCodeBase(), "BKG.png");
offscreenImage = createImage(width, height);
// offscr = offscreenImage.getGraphics();
Cart = getImage(getCodeBase(), "Untitled-1.png");
candy[0] = getImage(getCodeBase(), "candy1.png");
candy[1] = getImage(getCodeBase(), "candy2.png");
candy[2] = getImage(getCodeBase(), "candy3.png");
candyRemaining[0] = true;
candyRemaining[1] = true;
candyRemaining[2] = true;
candyX[0] = 100;
candyX[1] = 300;
candyX[2] = 400;
candyY[0] = 425;
candyY[1] = 0;
candyY[2] = 210;
}

public void paint(Graphics g) {
super.paint(g);
DecimalFormat df = new DecimalFormat("0.00");
Random generator = new Random();
if (hasStarted) {
if (inBounds) {
g.drawImage(Cart, XX, XY, this);
this.Candy(g);
if (left) {
XX -= 5;
if (XX < 0) {
XX += 5;
}
}
if (right) {
XX += 5;
if (XX > 500) {
XX -= 5;
}
}
if (down) {
XY += 5;
if (XX > 500) {
XY -= 5;
}
}
if (up) {
XY -= 5;
if (XY < 0) {
XY += 5;
}
}
} else {
GameRunning = false;
}
} else {
g.drawImage(Title, 0, 0, this);
}
}

public void Candy(Graphics g) {
for (int count = 0; count < 3; count++) {
if (candyRemaining[count]) {
g.drawImage(candy[count], candyX[count], candyY[count], this);
if (candyX[count] < XX + 50 && candyX[count] + 50 > XX) {
if (candyY[count] < XY + 50 && candyY[count] + 50 > XY) {
System.out.println(count);
candyRemaining[count] = false;
}
}
}
}
}

public void update(Graphics g) {
paint(g);
}

public void run() {
while (PlayAgain == true) {
while (GameRunning == true) {
repaint();
try {
Thread.sleep(2);
} catch (InterruptedException e) {
};
}
if (GameRunning == false) {
}
}
}

public void mouseDragged(MouseEvent ev) {
}

public void mouseMoved(MouseEvent ev) {
}

public void mouseClicked(MouseEvent ev) {
if (!hasStarted) {
hasStarted = true;
}
if (GameRunning == false) {
if (ev.getX() > 0 && ev.getX() < 1000 && ev.getY() > 0 && ev.getY() < 1000) {
GameRunning = true;
}
}
}

@Override
public void mouseEntered(MouseEvent arg0) {
onScreen = true;
inBounds = true;
}

public void mouseExited(MouseEvent ev) {
onScreen = false;
inBounds = false;
}

@Override
public void mousePressed(MouseEvent arg0) {
}

@Override
public void mouseReleased(MouseEvent arg0) {
}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("Pressed");
int key = e.getKeyCode();
if (key == e.VK_A) {
left = true;
}
if (key == e.VK_D) {
right = true;
}
if (key == e.VK_S) {
down = true;
}
if (key == e.VK_W) {
up = true;
}
}

@Override
public void keyReleased(KeyEvent e) {
System.out.println("Released");
int key = e.getKeyCode();
if (key == e.VK_A) {
left = false;
}
if (key == e.VK_D) {
right = false;
}
if (key == e.VK_S) {
down = false;
}
if (key == e.VK_W) {
up = false;
}
}

@Override
public void keyTyped(KeyEvent e) {
}
}

最佳答案

不要使用对象 e 作为键码,请使用 static KeyEvent 类。此外,如果您使用 switch 语句而不是 if 语句,您的代码看起来会更漂亮:

switch (e.getKeyCode()) {
case KeyEvent.VK_A: // here's the change
left = true;
break;

关于java - KeyPressed 在每个程序中都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24102222/

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