- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一点困难。我请你看一下代码:
1 个类 (MyCanvas.java)
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 490;
private static final int HEIGHT = 470;
public static InputKey input = new InputKey();
private int x = 10;
private int y = 10;
// public MyCanvas() {
// addKeyListener(input);
// }
public void move() {
if (x == 0) {
x = 10;
}
if (y == 0) {
y = 10;
}
if (input.left) {
x--;
}
if (input.right) {
x++;
}
}
public void paint(Graphics g) {
Image img1 = Toolkit.getDefaultToolkit().getImage(
"C:\\Users\\дНМ\\workspace\\Game\\image\\Peopl.png");
int width = img1.getWidth(this);
int height = img1.getHeight(this);
int scale = 4;
int w = scale * width;
int h = scale * height;
g.drawImage(img1, x, y, (int) w, (int) h, this);
}
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.getContentPane().add(new MyCanvas());
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
frame.setFocusable(true);
frame.requestFocusInWindow();
frame.addKeyListener(input);
}
}
第二类(InputKey.java)
package Game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
public class InputKey extends JComponent implements KeyListener {
private static final long serialVersionUID = 1L;
public boolean left;
public boolean right;
public MyCanvas cv;
void FBool() {
left = right = false;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
}
cv.move();
repaint();
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
//cv.move();
//repaint();
}
public void keyTyped(KeyEvent e) {
// bla...bla..bla
}
}
第一类工作完美,图片框也显示在其中。但是当我按下按钮(向左箭头或向右箭头)时,出现错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Game.InputKey.keyPressed(InputKey.java:28)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
请告诉我要在代码中修复哪些内容才能使其正常工作)
提前致谢,对于糟糕的设计表示歉意。
UPD
仍然没有弄清楚有点容易,但我想不是。如果有人写出一个现成的解决方案来解决问题 - 我会很高兴)
最佳答案
导致 NullPointerException 的问题是您没有为 cv 赋值。
我更改了代码,现在它可以工作了:
输入键
package help.stackoverflow.keyboard_awt;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
public class InputKey extends JComponent implements KeyListener {
private static final long serialVersionUID = 1L;
public boolean left;
public boolean right;
private MyCanvas cv;
public InputKey(MyCanvas cv){
this.cv = cv;
}
void FBool() {
left = right = false;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
}
cv.move();
repaint();
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
//cv.move();
//repaint();
}
public void keyTyped(KeyEvent e) {
// bla...bla..bla
}
}
我的 Canvas
package help.stackoverflow.keyboard_awt;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 490;
private static final int HEIGHT = 470;
private InputKey input = new InputKey(this);
private int x = 10;
private int y = 10;
// public MyCanvas() {
// addKeyListener(input);
// }
public void move() {
if (x == 0) {
x = 10;
}
if (y == 0) {
y = 10;
}
if (input.left) {
x--;
}
if (input.right) {
x++;
}
repaint();
}
public void paint(Graphics g) {
Image img1 = Toolkit.getDefaultToolkit().getImage(
"C:\\Users\\дНМ\\workspace\\Game\\image\\Peopl.png");
int width = img1.getWidth(this);
int height = img1.getHeight(this);
int scale = 4;
int w = scale * width;
int h = scale * height;
g.drawImage(img1, x, y, (int) w, (int) h, this);
}
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
MyCanvas myCanvas = new MyCanvas();
frame.getContentPane().add(myCanvas);
frame.getContentPane().setBackground(Color.BLACK);
frame.setVisible(true);
frame.setFocusable(true);
frame.requestFocusInWindow();
frame.addKeyListener(myCanvas.input);
}
}
还缺少我添加的 repaint()。
关于java - 使用键盘 ("AWT-EventQueue-0"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197170/
我不确定我做错了什么。 我创建了一个计时器,用于更新 GUI 顶部的文本图形。但是,当计时器用完时,它会更改一个变量(链接到一个监听器),并向我的 GUI 添加一个新的图形“场景/组/节点”。 -更改
我有一个带有 gui 的简单 JAVA 程序,它只递增 int 变量并在 JLabel 中显示其值。我通过使用 Runnable 类在其中调用 EventQueue.invokeLater() 来创建
我们遇到了一个问题,EventQueue 表有时增长得非常快,每秒最多 3k 条记录,而且从不清除记录(目前为 3000 万条)。我们的环境设置如下: 网站核心 7.2 4台CD服务器和1台CM服务器
我最近在我的机器上安装了 JDK 1.6 update 43。从那时起,每当我尝试运行事件处理程序时,我都会遇到一个问题,显示线程“AWT-EventQueue-0 中出现异常” “ 它指向哪个线程?
有人可以解释一下 EventQueue 对象在 Java Swing 中的用途吗? 我最困惑的是如何应用排队事件。也许提供事件队列的通用用途会有所帮助。 最佳答案 因为 Swing 是 single
我有一个外部设备一次向我发送 1 个字符的数据。我正在将其写入 JTextPane 上的 StyledDocument。这些数据是在不是 AWT 线程的线程上发送给我的,因此我需要创建 AWTEven
我有如下一段代码 import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import javax.swing.
我有一个扩展 JDialog 的登录表单,用户可以通过刷卡或输入用户名和密码登录。 我创建了一个 Runnable 守护程序,它与磁条阅读器通信,启动时它会请求刷卡,它会一直等到有人刷卡。如果应用程序
运行此代码时一切正常,但是当我更改内部代码以与 EventQueue.invokeLater() 一起使用时,主要问题出现了 package dav.gui; import java.awt.Bord
我有4个类(class) 用户界面/产品用户界面 Controller /产品 Controller 项目包/产品 项目包/ProductRecords 当我尝试按下“添加”按钮时,它返回一个错误,如
我对以下显示 EventQueue 行为不一致的示例代码有疑问: public static void main( String[] args ) throws InvocationTargetExc
我有一个 Java 程序占用了 100% 的 CPU,但似乎什么也没做。 如果我进行线程转储,则有 4 个线程(在 5 个线程池中)等待获取锁。 "Incoming WorkPool 5" - Thr
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
所以我试图弄清楚这个贪吃蛇游戏是如何运作的: http://zetcode.com/tutorials/javagamestutorial/snake/ 当我将代码复制到编译器中时,我在这一段代码中遇
有人可以帮我调试这个吗 编辑1:错误不在下面的代码中*以下代码中出现错误:* if (e.getSource().equals(btnRefresh)) {
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 9 年前。 Empl
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
只是尝试打印第 31 行的 LOL,或者任何人都有更好的方法从下面保存的文本文件中返回余额。请随意分享,我在这个问题上陷入了困境。出现错误: "Exception in thread "AWT-Eve
下午,我对一些 JFrame 代码有疑问,当用户按下“新用户”时,此 JFrame 就会启动,每当他们这样做时,我都会得到: Exception in thread "AWT-EventQueue-0
好的,我收到此错误 awt eventqueue 0 nullpointerexception 错误。当我尝试删除 JPanel 时。 令我困惑的是,当我删除另一个 JPanel 时,它工作得很好,但
我是一名优秀的程序员,十分优秀!