- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的项目中,我尝试从发送 writeInt(e.getKeyCode()) 的另一台计算机接收
到这台计算机,但是当我收到 dataInputStream
,即 readInt()
keyCode
时,如何将其放入 KeyEvent
对象中?
这是我的编码的一部分:
KeyEvent enemyKey;
try{
key = inputFromClient.readInt();
if(key!=0) {
enemyKey.setKeyCode(key);
player2.keyPressed(enemyKey);
}
}catch(Exception ex){
System.out.println(ex);
}
通过执行上面的代码,我得到 NullPointerException,因为 KeyEvent
heavenlyKey 为 null
。我该如何解决这个问题?
最佳答案
您还没有初始化KeyEventenemyKey;
您可以使用此构造函数初始化它
public KeyEvent(Component source,
int id,
long when,
int modifiers,
int keyCode,
char keyChar)
source - The Component that originated the event
id - An integer indicating the type of event. For information on allowable values, see the class description for KeyEvent
when - A long integer that specifies the time the event occurred. Passing negative or zero value is not recommended
modifiers - The modifier keys down during event (shift, ctrl, alt, meta). Passing negative value is not recommended. Zero value means that no modifiers were passed. Use either an extended _DOWN_MASK or old _MASK modifiers, however do not mix models in the one event. The extended modifiers are preferred for using
keyCode - The integer code for an actual key, or VK_UNDEFINED (for a key-typed event)
keyChar - The Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character)
参见KeyEvent javadoc有关可以传递给构造函数的值的更多信息
此外,请查看 Javabeans
关于java - 如何在Java中创建一个空的KeyEvent和SetKeyCode(keyCode)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439207/
我是一名优秀的程序员,十分优秀!