我正在尝试使用KeyListner
用左右键在游戏中向前和向后移动角色,但是我似乎一点也不移动角色。我运气不好看了这个答案:
How to use keyListener properly in Java和
How do i use KeyListener in java?
这是我的代码:
package test2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Test
{
public static void main(String[] args)
{
Frame A1 = new Frame();
}
}
class Frame
{
JFrame window;
public Frame ()
{
awt();
}
public void awt()
{
Design A2 = new Design()
{
@Override
public void keyTyped(KeyEvent e)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyPressed(KeyEvent e)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e)
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
window = new JFrame("Kyo's test");
window.setVisible(true);
window.setSize(1280, 786);
window.add(A2);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
abstract class Design extends JPanel implements ActionListener, KeyListener
{
// Timer objects for character and saws
Timer movementOfCharacter = new Timer(5, this);
Timer movementOfSaw = new Timer(15, this);
// Variables for movement
int xAxisOfCharacter = 50, velocityOfCharacter=2, yAxisOfSaw = 0, velocityOfSaw = 10;
private Image tree, background, character, saw;
// Constructor
Design()
{
movementOfCharacter.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
// Constructor of Graphics
protected void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
graphics2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
movementOfSaw.start();
// Body for background
{
ImageIcon whale = new ImageIcon(this.getClass().getResource("whale.jpg"));
background = whale.getImage();
graphics2d.drawImage(background, 0, 0, getWidth(), getHeight(), this);
}
// Body for trees
{
ImageIcon environment = new ImageIcon(this.getClass().getResource("Tree_Alt.png"));
tree = environment.getImage();
graphics2d.drawImage(tree, getWidth()/8, getHeight()-217, this);
graphics2d.drawImage(tree, (getWidth()/5)*2, getHeight()-217, this);
graphics2d.drawImage(tree, (getWidth()/6)*4, getHeight()-217, this);
}
// Body for Saws of death
{
ImageIcon sawOfDeath = new ImageIcon(this.getClass().getResource("saw.gif"));
saw = sawOfDeath.getImage();
// First saw
{
graphics2d.drawImage(saw, (getWidth()/11), yAxisOfSaw, 100, 100, this);
}
// Second saw
{
graphics2d.drawImage(saw, (getWidth()/7)*2, yAxisOfSaw, 100, 100, this);
}
// Thirf saw
{
graphics2d.drawImage(saw, (getWidth()/2)-10, yAxisOfSaw, 100, 100, this);
}
// Fourth saw
{
graphics2d.drawImage(saw, (getWidth()/6)*4, yAxisOfSaw, 100, 100, this);
}
// Fifth saw
{
graphics2d.drawImage(saw, (getWidth()/6)*5, yAxisOfSaw, 100, 100, this);
}
}
// Body for exit
{
graphics.setColor(Color.red);
graphics.fillRect(getWidth()-5, getHeight()-150, 5, 150);
}
// Body for Character
{
ImageIcon blanka = new ImageIcon(this.getClass().getResource("Blank.gif"));
character = blanka.getImage();
graphics2d.drawImage(character, xAxisOfCharacter, getHeight()-150, 150, 155, this);
}
}
public void actionPerformed(ActionEvent e)
{
yAxisOfSaw += velocityOfSaw;
if(yAxisOfSaw > 0 || yAxisOfSaw < getHeight()-100)
velocityOfSaw = -velocityOfSaw;
repaint();
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT)
{
velocityOfCharacter = -5;
}
if(key == KeyEvent.VK_RIGHT)
{
velocityOfCharacter = 5;
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
}
谁能告诉我我在哪里和哪里做错了吗?
最佳答案
问题
当你做
Design A2 = new Design() {
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
您正在使用抛出异常来覆盖在
Design
抽象类中编写的代码。解决方案
您的
Design
类不必是abstract
,因为它没有未实现的方法。因此,我可以看到两种使用Design
类中编写的代码的方式:第一种方式
删除
abstract
关键字,以便您可以像使用Design
关键字的普通类一样实例化new
类。因此,以这种方式声明Design
类:class Design extends JPanel implements ActionListener, KeyListener
然后以这种方式实例化A2:
Design A2 = new Design();
第二种方式
如果出于某种原因,您需要将该类抽象化,请按照以下方式实例化您的A2对象:
Design A2 = new Design() {};
这将起作用,并且不会覆盖您在
Design
类中编写的任何方法。但这没有什么意义,上面的第一种方式更有意义。笔记
注意1
现在,如果您在
Design.keyPressed
中添加打印输出,则可以看到它被调用了(您将在控制台中看到日志):public void keyPressed(KeyEvent e) {
System.out.println("KeyEvent: "+e);
int key = e.getKeyCode();
...
注意2
同时查看
keyPressed
方法,我认为您还需要更新字符X轴坐标,因此在if语句后添加以下行:xAxisOfCharacter += velocityOfCharacter;
它看起来应该像这样:
public void keyPressed(KeyEvent e) {
System.out.println("KeyEvent: "+e);
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT) {
velocityOfCharacter = -5;
}
else if(key == KeyEvent.VK_RIGHT) {
velocityOfCharacter = 5;
}
xAxisOfCharacter += velocityOfCharacter;
}
关于java - 如何在Java中正确使用Keylistener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49852803/