键盘事件Java [英] Keyboard events java

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

问题描述

我最近开始学习Java.我想制作一个类似的游戏https://sites.google.com/site/millseagles/home/Games/multiplayer/tron 我曾经使用简单的图形库在c ++中实现它.我打算将图形部分放下来,我打算使用小图像并使用 http://horstmann.com/sjsu/graphics/这个基本的图形库.我想不出我想要的键盘输入,因此如果按箭头,图片会添加一个小的绿色方块(我有一个green.png).键盘监听器.我收到所有这些错误.我只需要一个简单的库,我可以说getKey()之类的东西,并且可以使用if()来找出动作.这就是我的代码.事件,但不了解.

I have recently started learning java.I want to make a game like https://sites.google.com/site/millseagles/home/Games/multiplayer/tron I have made it in c++ once using a simple graphics lib. I have the graphics part down i plan to use small images and use http://horstmann.com/sjsu/graphics/ this basic graphics lib.I can't figure out keyboard input i want it so if you press an arrow the picture adds a small green square(I have a green.png).I can't figure out to use keyboard listeners.I get all these errors.I just need a simple lib that i can say getKey() or something and i can use if() to figure out the action.this is the code I have.I was messing with the key event but don't understand it.

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

 public class game implements KeyListener
 {

public void keyReleased(KeyEvent e){} 

public void keyTyped(KeyEvent e){} 
public game()//snake like game
{

}
public  void test() 
{

    int x=30,y=30;//middle total 60x60

    tile[] map=new tile[3600];//tile is a class i made that is a picture and some int and bool using the simple lib i linked 60 by 60 tiles
    for(int i=0;i<3600;i++)
    {
        map[i]=new tile();
    }


}

 public void keyPressed(KeyEvent e)//this does not work i want it to work when a key is clicked
 { 
     while(x>0)//this part works when it is not in the keypressed function
       {

        map[(y*60)+x].load(4);//4 refrences a green rectangle image
        map[(y*60)+x].draw(x,y,10);//draw it based on x and y 10 pixels sized tiles
       x--;//make a line going left 



       }

} 
}

我知道这可能是一团糟.我已经测试了我的代码,它的工作原理在尝试实现键盘事件时会中断.如果您可以将我指向一个对初学者更友好的库,那会很棒.

I know this may be messy.I have tested my code it works it just breaks when i try to implement keyboard events.If you can point me to a much more beginner friendly lib that would be great.

推荐答案

您只需要将侦听器添加到某物(例如正在玩游戏的窗口).

You simply have to add the listener to something (e.g. the window where the game is being played).

我将举一个例子,在这里我们将仅显示被敲击的键的代码.

I will give you an example, where we will simply display the code of the key being stroked.

这是您生成接口的类:

import java.awt.Dimension;
import javax.swing.JFrame;

public class Game {

    public static void main(String[] args) {
        /* Creating a window (300x400) */
        JFrame frame = new JFrame("Add your own title");
        frame.setPreferredSize(new Dimension(300, 400));

        /* This is the part where we add the keyListener (notice that I am also sending
         * this window as a parameter so that the listener can modify it)*/
        frame.addKeyListener(new ArrowListener(frame));

        /* Making the window visible */
        frame.pack();
        frame.setVisible(true);
    }
}

这是我们拥有侦听器的类:

And this is the class where we have the listener:

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

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ArrowListener implements KeyListener {
    /* We keep the window as an instance variable so we can modify it once the event is triggered */
    JFrame frame;

    /* This is the constructor */
    public ArrowListener(JFrame j) {
        frame = j;
    }

    /* This is where the magic happens */
    public void keyPressed(KeyEvent e) {
        /* Modify this with what you actually want it to do */

        /* We clear the panel so we can add new text without any other text behind it */
        frame.getContentPane().removeAll();

        /* We add some text that actually shows the keyCode (left arrow = 37, top = 38, right = 39, bottom = 40) */
        frame.add(new JLabel("Key Code #" + String.valueOf(e.getKeyCode())));

        /* Redrawing the window */
        frame.revalidate();
    }

    /* These two are part of the contract we made when we decided to
     * implement the KeyListener */
    public void keyTyped(KeyEvent e) { /* Do nothing */ }
    public void keyReleased(KeyEvent e) { /* Do nothing */ }
}

注意:运行程序时,请按键以查看窗口中显示的文本.

Note: when running the program press the keys to see the text appearing on the window.

我没有走遍您正在使用的库,但是我使用了最受欢迎的库,称为swing(

I didn't get around the library you were using, but I used the most popular one called swing (tutorial)

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

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