如何检测Java中的按键 [英] How to detect a key press in Java

查看:227
本文介绍了如何检测Java中的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如何在java中检测到一个按键,我的IDE被称为eclipse,我已经做了一些类。我在互联网的 keyconfigofplayer 中找到了代码,并将其改为扩展播放器类。我想要的是,当我按住一个矩形或椭圆形的键将移动到屏幕上,我该怎么办?由于某些原因,它不会正确键入代码,这就是为什么我的导入没有星号并且看起来更好。对于那个很抱歉。请忽略它。我不能修复(
这里是我的代码:



代码窗口:

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

public class window {

public static void main(String [] args){
// TODO自动生成的方法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);

}
}






  //这个类是播放器图形

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

public class player extends JPanel {

int x = 50;
int y = 50;

public void paint(){//这是图形被画到屏幕的地方
repaint();
}

public void paintComponent(Graphics g){
//这是图形被制造的地方
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);

}
}






$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

$ b import javax。摇摆。*;
import java.awt。*;

public class player extends JPanel {

int x = 50;
int y = 50;

public void paint(){//这是图形被画到屏幕的地方
repaint();
}

public void paintComponent(Graphics g){
//这是图形被制造的地方
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);
}
}

我已经尝试过评论,所以你知道这是什么意思。
我从youtube频道macheads101得到keyconfigofplayer的代码如何在java插曲12或11中编程GUI我想。我认为我的问题不在于更新图像,而是关于检测按键被释放的时间。所以也许我应该做一个循环,表示:
如果这个键被按下,+ 10到x坐标,直到它完成,并不断更新位置。
我也想问一下,何时使用public,private和void?我不明白那些:(如果我完成了扩展播放器正确的实现,我试图改变macheads101的代码仔细,当我复制它完全没有工作,所以我真的不知道是什么错,我会感谢如果你回答了我的问题,并帮助我改变了我的代码,最后它将成为一个射手,像街机游戏太空入侵者或龙射手。

解决方案

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

这篇关于如何检测Java中的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆