Java 使用键盘(“AWT-EventQueue-0”)

Java 使用键盘(“AWT-EventQueue-0”),java,swing,nullpointerexception,awt,keylistener,Java,Swing,Nullpointerexception,Awt,Keylistener,有点困难。请大家看一下代码: 1类(MyCanvas.java) 第二个类(InputKey.java) First class功能完美,相框也显示在其中。但当我按下按钮(左箭头或右箭头)时,我得到一个错误: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Game.InputKey.keyPressed(InputKey.java:28) at java.awt.Componen

有点困难。请大家看一下代码:

1类(MyCanvas.java)

第二个类(InputKey.java)

First class功能完美,相框也显示在其中。但当我按下按钮(左箭头或右箭头)时,我得到一个错误:

 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
    }
}
我的画布

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()。

您从未为
cv
赋值,所以它总是空的…所以我们必须在其他地方使用它。她应该赋值什么?是吗新的MyCanvas();对于Swing,通常在基于AWT的较低级别上使用键绑定,
KeyListener
。有关如何使用它们的详细信息,请参阅。有关更多想法,请参阅。
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
    }
}
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);
    }

}