How do I call KeyListener and use KeyEvent?

2019-08-10 21:33发布

问题:

I am trying to design a simple box that moves around on the screen. I intend to paint it, then when ever I press an arrow key it will change its position and then repaint it. It will keep doing this until it reaches the outer borders, then the code will stop. But I do not know how to create a KeyListener to check for a keytyped. There may be a better way to do this than what I am trying to do, any help would be greatly appreciated.

Okay thank you those ideas do help me, but i am also having an issue with the same code... I do not know how to call make a KeyEvent to call the method check if the arrow keys are pressed. Like writing the code in to move the character would not be too hard, but how would I call the method? I need a keyEvent as a parameter but I do not know how to call that. I mewant to put this in my question before but I forgot, I am sorry.

main 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 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;
    }
}