gpt4 book ai didi

java - 简单的 KeyListener 应用程序出现错误

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

基本上我是一个初级程序员,我对 C#、C++ 略知一二现在我正在学习 Java 来构建 Android 应用程序。我正在从brandonio制作的youtube学习java,非常有帮助。 http://www.youtube.com/user/BrandonioProductions?feature=watch

这是完整的代码:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class KeyListenerApp
extends Applet
implements KeyListener{
private Rectangle rect; //The rectangle that moving
private ArrayList<Integer> keysDown;
public void init()
{
rect = new Rectangle(0,0,50,50);
this.addKeyListener(this);
}
public void paint(Graphics g)
{
setSize(500,500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
if(keysDown.contains(new Integer(e.getKeyCode())))
{
keysDown.add(new Integer(e.getKeyCode()));
}
moveRect();
}
@Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveRect()
{
int x = rect.x;
int y = rect.y;
if (keysDown.contains(KeyEvent.VK_UP))
y -= 2;
if (keysDown.contains(KeyEvent.VK_DOWN))
y += 2;
if (keysDown.contains(KeyEvent.VK_LEFT))
x -= 2;
if (keysDown.contains(KeyEvent.VK_RIGHT))
x += 2;
rect.setLocation(x, y);
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
}

这是完整的错误:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at KeyListenerApp.keyPressed(KeyListenerApp.java:34)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at KeyListenerApp.keyReleased(KeyListenerApp.java:44)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

很抱歉一开始没有在此处发布代码,我认为如果将代码与问题分开,您会更舒服。

谢谢

这是工作代码:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;


public class KeyListenerApp
extends Applet
implements KeyListener{

private Rectangle rect; //The rectangle that moving

private ArrayList<Integer> keysDown;

public void init()
{
rect = new Rectangle(0,0,50,50);
this.addKeyListener(this);
keysDown = new ArrayList<Integer>();
}

public void paint(Graphics g)
{
setSize(500,500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(rect);
}

@Override
public void keyPressed(KeyEvent e) {

if(keysDown.contains(new Integer(e.getKeyCode())) == false)
{
keysDown.add(new Integer(e.getKeyCode()));
}

moveRect();
}

@Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));

}

public void moveRect()
{
int x = rect.x;
int y = rect.y;

if (keysDown.contains(KeyEvent.VK_UP))
y -= 2;

if (keysDown.contains(KeyEvent.VK_DOWN))
y += 2;

if (keysDown.contains(KeyEvent.VK_LEFT))
x -= 2;

if (keysDown.contains(KeyEvent.VK_RIGHT))
x += 2;

rect.setLocation(x, y);
repaint();
}

@Override
public void keyTyped(KeyEvent e) {


}

}

导致错误的原因:

if(keysDown.contains(new Integer(e.getKeyCode())) == false)
{
keysDown.add(new Integer(e.getKeyCode()));
}

在我修复它之前,这段代码是这样的:

if(keysDown.contains(new Integer(e.getKeyCode())))
{
keysDown.add(new Integer(e.getKeyCode()));
}

发生错误是因为我在代码中说:如果有东西存在则添加他,并且只有当他不存在时才添加他。

最佳答案

private ArrayList<Integer> keysDown;

您永远不会创建此对象的实例,因此会出现 NullPointerException

关于java - 简单的 KeyListener 应用程序出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16362700/

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