简体   繁体   English

每次按下空格键时在 Java 中增加一个变量

[英]increment a variable in Java when every time the space bar is pressed

I have been trying to increment a variable every time the space bar is pressed in Java.每次在 Java 中按下空格键时,我都试图增加一个变量。

The code I wrote is below but it seems to be wrong as it increments without pressing the key.我写的代码在下面,但它似乎是错误的,因为它不按键就增加了。

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

public class TestKeyPress {
    public static void main(String[] args) {
        
        System.out.println(keyPressed(null));
        
    }
    public static int keyPressed(KeyEvent e) {
        int var = 0;
        while (var <6) {
            var ++;
            System.out.println(hi);
        }
        System.out.println("max reached");
        return var;
    }
}

You need to bind the listener into an event.您需要将侦听器绑定到一个事件中。 You cannot expect anything happens when the passed KeyEvent is null .当传递的KeyEventnull时,您不能期望发生任何事情。 Moreover, you don't specify anywhere the space bar is pressed.此外,您没有指定按下空格键的任何位置。 You also miss the context to which is the key-press event registered.您还会错过注册按键事件的上下文。 Is it a console or a desktop application?它是控制台还是桌面应用程序? As long as you use java.awt.event I suspect the latter.只要你使用java.awt.event我怀疑是后者。 Here is a minimal sample:这是一个最小的示例:

Java Desktop application using Swing使用 Swing 的 Java 桌面应用程序

public static void main(String[] argv)  {

    JTextField textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {

        int counter = 0;
 
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                if (counter < 6) {
                    counter++;
                    System.out.println("hi");
                } else {
                    System.out.println("max reached");
                }
            }
        }
    });

    JFrame jframe = new JFrame();
    jframe.add(textField);
    jframe.setSize(640, 320);
    jframe.setVisible(true);
}

Console application控制台应用程序

In that case, you cannot use anything from the java.awt.* or javax.swing.* libraries and you have to stick with classes from java.io.* .在这种情况下,您不能使用java.awt.*javax.swing.*库中的任何内容,而必须坚持使用java.io.*类。

Because when the keyPressed called the var variable will be zero, you should declare outside the keyPressed .因为当keyPressed调用 var 变量将为零时,您应该在keyPressed之外声明。

Also if you want to bind with the progress bar you should implement the listener.此外,如果您想与进度条绑定,您应该实现侦听器。

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

public class TestKeyPress {

    private static int var = 0;

    public static void main(String[] args) {
        System.out.println(keyPressed(null));
    }

    public static int keyPressed(KeyEvent e) {
        while (var < 6) {
            var ++;
            System.out.println(hi);
        }
        System.out.println("max reached");
        return var;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM