gpt4 book ai didi

java - KeyListener 不工作(requestFocus 没有修复它)

转载 作者:行者123 更新时间:2023-11-29 07:07:39 25 4
gpt4 key购买 nike

我正在尝试在没有 YouTube 视频帮助的情况下制作我的第一款游戏。我看过很多关于制作 java 游戏的 youtube 系列,但它们从来没有出现在我想做的地方,所以在学习足够多之后,我开始制作自己的游戏。我对 Java 有足够的了解,到目前为止,一切都没有问题。

我曾尝试寻求一些帮助,但我只能找到人们说尝试以下内容的地方:

setFocusable(true);
requestFocus();

然而,这对我不起作用。我知道大多数人不喜欢只浏览我的所有代码,但我认为它是易于管理的(代码还不是那么多)。

我打算把它做成一种蛇类游戏,你会变大而不是变长,你是一只豚鼠(现在用橙色矩形表示)而不是蛇。

游戏.java:

package com.kaperly.game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame {
private static final long serialVersionUID = 1L;

public static JFrame frame;
public static JPanel panel;

public static int width = 1000;
public static int height = 600;

public Game() {
frame = new JFrame();
panel = new Panel();

frame.setPreferredSize(new Dimension(width, height));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Guinea Pig Eat");
frame.setLayout(new BorderLayout());
frame.setVisible(true);
panel.setFocusable(true);
panel.requestFocus(true);

frame.add(panel, BorderLayout.CENTER);
frame.pack();
}

public static void init() {

}

public static void loop() throws InterruptedException { while(true) {
Character.move();

Thread.sleep(10);
}}

public static void main(String[] args) throws InterruptedException, IOException {
Game game = new Game();

init();
loop();
}
}

面板.java:

package com.kaperly.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Panel extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;

public static Image startButton;

public static boolean goingRight = false;
public static boolean goingLeft = false;
public static boolean goingUp = false;
public static boolean goingDown = false;

private static int changeDirection = 0;

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W) {
goingUp = true;
changeDirection = 1;
System.out.println("Moved Up!");

}else if(e.getKeyCode() == KeyEvent.VK_A) {
goingLeft = true;
changeDirection = 2;

}else if(e.getKeyCode() == KeyEvent.VK_S) {
goingDown = true;
changeDirection = 3;

}else if(e.getKeyCode() == KeyEvent.VK_D) {
goingRight = true;
changeDirection = 4;

}else if(changeDirection == (1 | 2 | 3 | 4)) {
changeDirection = 0;
if(changeDirection == 1) {
goingLeft = false;
goingDown = false;
goingRight = false;

}else if(changeDirection == 2) {
goingUp = false;
goingDown = false;
goingRight = false;

}else if(changeDirection == 3) {
goingUp = false;
goingLeft = false;
goingRight = false;

}else if(changeDirection == 4) {
goingUp = false;
goingLeft = false;
goingDown = false;
}
}
}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {

}

public void paintComponent(Graphics g) {
super.paintComponent(g);

//Set background color
g.setColor(Color.GRAY);
g.fillRect(0, 0, Game.width, Game.height);


//Make Walls
g.setColor(Color.BLACK);
g.fillRect(0, 0, Game.width, 25);
g.fillRect(0, 550, Game.width, 25);
g.fillRect(0, 0, 25, Game.height);
g.fillRect(970, 0, 25, Game.height);

//Show Character
g.setColor(Color.ORANGE);
g.fillRect(Character.x, Character.y, Character.width, Character.height);

repaint();
}
}

字符.java:

package com.kaperly.game;

public class Character {

public static int x = 475;
public static int y = 250;

public static int width = 50;
public static int height = 75;

public static void move() {
if(Panel.goingUp == true) {
y = y ++;
}
if(Panel.goingLeft == true) {
x = x --;
}
if(Panel.goingDown == true) {
y = y --;
}
if(Panel.goingRight == true) {
x = x ++;
}
}
}

最佳答案

KeyListener 应该避免,因为它存在与焦点相关的固有问题。您永远不知道什么可能会将键盘焦点从您的组件上移开。

相反,利用 Key Bindings API,它允许您控制触发关键事件所需的焦点级别。

它还允许您在其他地方重用与键绑定(bind)关联的底层 Action...

您最好将类 Panel 重命名为更有意义的名称。 AWT 已经有一个 Panel 类,很容易混淆这两者

关于java - KeyListener 不工作(requestFocus 没有修复它),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18029136/

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