且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测Java中的按键

更新时间:2022-10-30 21:42:49

阅读关于 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){
}
});


My son (aged 11) who is learning Java is going to enter a question and some code. He tells me he has not been able to find the answer to this question on the site. You will be reading my edited version of his question. Many thanks.

How can I detect a keypress in java, My IDE is called eclipse and I have made some classes. I found the code inside keyconfigofplayer of the internet and changed it to extend player class. I want it so when I hold down a key a rectangle or oval will move across the screen, how could I do this? For some reason it wont type the code correctly which is why my imports dont have the asterisk and look wierd. sorry about that. please ignore it. I cant fix that:(. here is my code:

code for window:

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);

    }
}


//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);

    }
}


//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);
    }
}

The classes I have tried to comment well so you know what it means. I got keyconfigofplayer's code from the *** channel macheads101 how to program GUI in java episode 12 or 11 I think. I think my problem isn't about updating the image, but about detecting when the key is pressed and then released. So maybe I should make a while loop stating that: if this key is pressed down, + 10 to the x coordinates until its done and constantly update the position. I would also like to ask, when to use public, private and void? I dont understand those :( and if Ive done the extends player implements thing correctly. I tried to alter macheads101's code carefully, when I copied it exactly it didn't work too. So I really dont know what is wrong. I would be thankfull if you answered my question and helped me to alter my code. In the end its gonna be a shooter, like the arcade game 'space invaders' or 'dragon shooter'.

Read about java.awt.event.KeyListener

A code should look like this:

    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) {
        }
    });