如何在Java中检测按键

use*_*497 9 java eclipse swing keypress keylistener

我的儿子(11岁)正在学习Java,他将输入一个问题和一些代码.他告诉我他无法在网站上找到这个问题的答案.您将阅读我的问题的编辑版本.非常感谢.

如何在java中检测到按键,我的IDE被称为eclipse,我已经创建了一些类.我在互联网的keyconfigofplayer中找到了代码并将其更改为扩展播放器类.我想要它,所以当我按住一个矩形或椭圆形键移动屏幕时,我怎么能这样做?由于某种原因,它不会正确输入代码,这就是为什么我的导入没有星号,看起来很奇怪.对于那个很抱歉.请忽略它.我无法解决这个问题:(.这是我的代码:

窗口代码:

import javax.swing.*;
import java.awt.*;

public class window {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(600, 600);
        player p = new player();
        f.add(p);

        keyconfigofplayer key = new keyconfigofplayer();
        p.add(key);
        f.add(key);

    }
}
Run Code Online (Sandbox Code Playgroud)
//this class is the player graphic

import javax.swing.*;
import java.awt.*;

    public class player extends JPanel {

    int x = 50;
    int y = 50;

    public void paint() {//This is where the graphic is painted to the screen
        repaint();
    }

    public void paintComponent(Graphics g) {
        //This is where the graphic is 'manufactured'
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        int width = 20;
        int height = 20;
        g.fillRect(x, y, width, height);

        int width2 = 10;
        int height2 = 10;
        g.fillOval(x, y, width2, height2);

    }
}
Run Code Online (Sandbox Code Playgroud)
//the keyconfigofplayer class(where i think the problems are):
//this class is the player graphic

import javax.swing.*;
import java.awt.*;

    public class player extends JPanel {

    int x = 50;
    int y = 50;

    public void paint() {//This is where the graphic is painted to the screen
        repaint();
    }

    public void paintComponent(Graphics g) {
        //This is where the graphic is 'manufactured'
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        int width = 20;
        int height = 20;
        g.fillRect(x, y, width, height);

        int width2 = 10;
        int height2 = 10;
        g.fillOval(x, y, width2, height2);
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的课程很好,所以你知道这意味着什么.我从youtube频道macheads101获得了keyconfigofplayer的代码,我认为如何在java第12集或第11集中编程GUI.我认为我的问题不是更新图像,而是检测按键何时被释放.所以也许我应该做一个while循环说明:如果按下这个键,+ 10到x坐标直到完成并不断更新位置.我还想问一下,何时使用公共,私有和无效?我不理解那些:(如果我已经完成了扩展播放器正确实现的东西.我试图仔细改变macheads101的代码,当我复制它时它确实不起作用.所以我真的不知道什么是错的.我会感谢你如果你回答我的问题并帮助我改变我的代码.最后它会成为一个射击游戏,比如街机游戏"太空入侵者"或"龙射手".

小智 13

阅读 java.awt.event.KeyListener

代码应如下所示:

    f.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("Key pressed code=" + e.getKeyCode() + ", char=" + e.getKeyChar());
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
Run Code Online (Sandbox Code Playgroud)


Pau*_*tha 5

因此GUI是事件驱动的.发生的一切都是由事件驱动的.按键是一个事件.组件需要侦听事件,并在事件触发时执行某些操作.您可以在此处阅读有关如何编写不同侦听器的更多信息.一些基本的听众很容易识别(通过名字)像KeyListenerMouseListener.ActionListener按钮按下也很常见.您将需要阅读教程并学习不同的听众.

请注意,虽然使用Swing时存在使用a的焦点问题,KeyListener因此最好使用键绑定.如KeyListener教程中所述:

要定义对特定键的特殊反应,请使用键绑定而不是键侦听器

一种键绑定的教程可以发现这里和一个例子参见这里.这是示例中的gif,使用键绑定按键显示矩形的移动

在此输入图像描述


更新只是注意到.

  1. 不要覆盖paint方法JPanel
  2. 永远不要repaint()paint/paintComponent方法内部打电话

    public void paint() {//This is where the graphic is painted to the screen
        repaint();
    }
    
    Run Code Online (Sandbox Code Playgroud)

完全摆脱上面的代码.