java检测按键,如何检测Java中的按键

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 youtube 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) {

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值