如何调用KeyListener并使用KeyEvent?

时间:2014-03-28 17:14:20

标签: java swing keyboard keylistener keyevent

我正在尝试设计一个在屏幕上移动的简单盒子。我打算画它,然后当我按箭头键时它会改变它的位置然后重新绘制它。它将一直这样做,直到它到达外边界,然后代码将停止。但我不知道如何创建KeyListener来检查密钥类型。可能有一种更好的方法来做到这一点,而不是我想做的事情,任何帮助将不胜感激。

好的,谢谢你这些想法对我有帮助,但我也遇到了相同代码的问题...我不知道如何调用make一个KeyEvent调用方法检查是否按下了箭头键。就像编写代码来移动角色一样不会太难,但是如何调用该方法呢?我需要一个keyEvent作为参数,但我不知道如何调用它。我之前想把它放在我的问题中,但我忘了,我很抱歉。

主要课程:

import javax.swing.JFrame;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.BorderLayout; 
import java.awt.Canvas;
//import java.awt.event.KeyEvent;
import java.awt.event.*;

public class CharacterTest extends JFrame {

    /**
     * Creates a new instance of <code>CharacterTest</code>.
     */
    public CharacterTest() 
    {
        super("Character Test");            
        setSize(800, 800);                  //create the window         
        MCharacter C = new MCharacter();    //call the box so that it can be affected           
        getContentPane().add(C);            
        setVisible(true);                   //show the window

        /*
         Here I need to create a while loop using the determine method (while determine is false check for keytyped in MCharacter 
         so that it keeps checking for a key.
         I need to create a keyListener within the while loop and then depending on what key is pressed it will move.
         */

         while(C.determine(C.getX(), C.getY()) == false)
         {
            //if up arrow key pressed, C.setY(C.getY()-2);
            //if down arrow key pressed, C.setY(C.getY()+2);
            //if right arrow key pressed, C.setX(C.getX()+2);
            //if left arrow key pressed, C.setX(C.getX()-2);
         }
    }

    public void Movement(KeyEvent key)
    {

    }

    public static void main(String[] args) {
        // TODO code application logic here
        CharacterTest test = new CharacterTest();
    }
}

MCharacter class:

    import javax.swing.JFrame;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.awt.Color;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import java.awt.BorderLayout; 
    import java.awt.Canvas;
    //import java.awt.event.KeyEvent;
    import java.awt.event.*;

    public class MCharacter extends Canvas {

    //these will be the instance variables for the character's parameters- its size and its location
    private int width;
    private int height;
    private int x;
    private int y;

    //some constructors that would easily be called
    public MCharacter() 
    {
        setBackground(Color.WHITE);
        setWidth(10);
        setHeight(10);
        setX(400);
        setY(400);
    }

    public MCharacter(int wth, int hgt)
    {
        setWidth(wth);
        setHeight(hgt);
        setX(400);
        setY(400);
    }

    public MCharacter(int wth, int hgt, int xPos, int yPos)
    {
        setWidth(wth);
        setHeight(hgt);
        setX(xPos);
        setY(yPos);
    }

    //setters for each of the variables
    public void setWidth(int wth)
    {
        width = wth;
    }

    public void setHeight(int hgt)
    {
        height = hgt;
    }

    public void setX(int xPos)
    {
        x = xPos;
    }

    public void setY(int yPos)
    {
        y = yPos;
    }

    //getters for each of the varaibles
    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    //I am going to call the paint method here and create a small box that I will use as the character
    public void paint(Graphics window)
    {
        window.setColor(Color.BLUE);
        window.fillRect(x, y, width, height);
    }

    //this method will be used to keep checking for a key pressed: while this is false check for a keytyped
    public boolean determine(int x, int y)
    {
        if( x >= 10 && x <= 790 && y >= 10 && y <= 790)
        {
            return false;
        }
        return true;
    }
}

0 个答案:

没有答案