使用keyListener的Java keyinput无法正常工作。我不知道我写错了什么

时间:2014-12-11 08:48:29

标签: java eclipse swing keylistener keyevent

我正在创建一个基本的cookieclicker游戏,我已经实现了一个keyinput(空格键)。现在我遇到了一个我无法解决的问题,eclipse说在运行之前没有任何问题。

Keyinput代码:

package Tryout;

import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Key_Input implements KeyListener {

    private boolean[] keys = new boolean[10];

    public boolean space;

    public void update(){

        space = keys[KeyEvent.VK_SPACE];

    }

    @Override
    public void keyPressed(KeyEvent e) {
        keys[e.getKeyCode()] = true;

    }

    @Override
    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;

    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}

然后是“主要代码”:

    package Tryout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

import Tryout.Key_Input;

public class adamTryout {

    private static int width = 600;
    private static int height = 338;

    static JLabel textLabel = new JLabel();

    static public Key_Input key;

    public static void createWindow() {

        JFrame frame = new JFrame("CookieClicker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);


        textLabel = new JLabel("CookieClicker" ,SwingConstants.CENTER); 
        textLabel.setPreferredSize(new Dimension(600, 338));

        frame.getContentPane().add(textLabel, BorderLayout.CENTER);



        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        createWindow();



        double x = 0;               //Number of Cookie Clickers

        double z;                   // Cost of the Cookie Clickers

        double cookies = 0;         //Amount of Cookies

        double count = 0;           //Additional amount of cookies per second from the Cookie Clickers

        final double constant = 0.1; //Amount of CPS (cookies per second) you get from 1 Cookie Clicker



        while (1>0) {               // the game loop, (infinity loop)
            key.update();          // Code that can't run

            if(key.space) {        // Code that can't run
                x++;
            }

            z = Math.ceil(15*java.lang.Math.pow(1.15, x));

            if(cookies >= z) {  //If you got enough cookies you autobuy a new CC and the number of CC's increase as well as the cost of a CC

                textLabel.setText("You bought a new Cookie Clicker; +0.1 cookie each second!");

                cookies = cookies - z;

                x++;

                try {  //delayer (Uptades 1 time per second)
                    Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }

            }


            try {  //delayer (Uptades 1 time per second)
                Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }

            count = x*constant;

            cookies = cookies + 1 + count;

            cookies = (double)Math.round(cookies*100)/100;

            textLabel.setText("cookies; " + cookies + " \n " + "CPS; " + ((double)Math.round((count + 1)*100)/100) + " \n " + "Cookie Clickers; " + Math.round(x));         // Your current amount of cookies
        }
    }
}

JFrame启动但控制台输出为:

Exception in thread "main" java.lang.NullPointerException at Tryout.adamTryout.main(adamTryout.java:59)

我根本不知道现在该做什么。请帮忙。

1 个答案:

答案 0 :(得分:1)

首次在线上使用变量键

时,不会初始化变量键
key.update();

实际上它根本就没有被初始化......