日期:2014-05-20  浏览次数:20731 次

送分来了,自定一个按键方法是怎么做的?
在学Java时,发现JAVA中 KeyEvent处理一直按下一个键时,处理一次之后要停顿一下才继续处,我上网找了找,最后就只看到一个网友回答说可以自定一个按键方法,在线程中调用它就行了,但得先把按键的状态传过去才行。看了不明白是怎么回事,所以来问问,
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Round extends JPanel implements KeyListener
{
int x=0;
int y=0;
public void paint(Graphics g)
{
super.paint(g);
super.paintComponent(g);
g.fillOval(x,y,20,20);


//I.setBounds(x,y,icon.getIconWidth(),icon.getIconHeight());
}
public void keyTyped(KeyEvent e) 
{
}
public void keyPressed(KeyEvent e)
{//按下按移动
if((e.getKeyCode()==KeyEvent.VK_A||e.getKeyCode()==KeyEvent.VK_LEFT)&&x>-10)
{
x-=2;
this.repaint();
}
if((e.getKeyCode()==KeyEvent.VK_D||e.getKeyCode()==KeyEvent.VK_RIGHT)&&x<878)
{
x+=2;
this.repaint();
}
if((e.getKeyCode()==KeyEvent.VK_W||e.getKeyCode()==KeyEvent.VK_UP)&&y>-10)
{
y-=2;
this.repaint();
}
if((e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_DOWN)&&y<878)
{
y+=2;
this.repaint();
}
}
public void keyReleased(KeyEvent e) 
{
}
}
//实现
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Realize implements KeyListener
{
JFrame j=null;
Round b=null;
public Realize()
{
j=new JFrame();
b=new Round();
j.setSize(888,888);
j.add(b);
j.addKeyListener(b);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
}
public static void main(String args[])
{
Realize r=new Realize();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) 
  {
  }
}
我发现我的也是,在移动时会动一下然后停一下再动,而且当我按了一个键没放开,再按一个键时可以动,但一放开后,虽然前一个键还按着,但却不会动了,请教一下懂的人该怎么做的。
//如果能给出直接相关的类名、接口名等让我看jdk或介绍些书看就更好了……

------解决方案--------------------
说说我的建议 将按下时所要执行的坐标变换写入线程 然后弹起时将线程结束
可以利用线程sleep()的方法来控制你按下后坐标变换的频率 而且利用同步可以避免当一个键按下时按另一个键同样会控制目标的问题
------解决方案--------------------
我照着你说的思路做了一下,KeyListener记录当前按键状态,线程根据状态重画图像

Round.java
Java code

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

public class Round extends JPanel implements KeyListener, Runnable
{
    public static final int UP = 1;
    public static final int DOWN = 2;
    public static final int LEFT = 3;
    public static final int RIGHT = 4;
    public static final int NULL = 5;
    
    int forward = NULL;
    int x=0;
    int y=0;
    
    public void paint(Graphics g)
    {
        super.paint(g);
        super.paintComponent(g);
        g.fillOval(x,y,20,20);
        
        
        //I.setBounds(x,y,icon.getIconWidth(),icon.getIconHeight());
    }
    
    public void keyTyped(KeyEvent e) 
    {
    }
    
    public void keyPressed(KeyEvent e)
    {//按下按移动
        if((e.getKeyCode()==KeyEvent.VK_A||e.getKeyCode()==KeyEvent.VK_LEFT)&&x>-10)
        {
            /*x-=2;
            this.repaint();*/
            if (forward == NULL)
            {
                forward = LEFT;
            }
        }
        else if((e.getKeyCode()==KeyEvent.VK_D||e.getKeyCode()==KeyEvent.VK_RIGHT)&&x<878)
        {
            /*x+=2;
            this.repaint();*/
            if (forward == NULL)
            {
                forward = RIGHT;
            }
        }
        else if((e.getKeyCode()==KeyEvent.VK_W||e.getKeyCode()==KeyEvent.VK_UP)&&y>-10)
        {
            /*y-=2;
            this.repaint();*/
            if (forward == NULL)
            {
                forward = UP;
            }
        }
        else if((e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_DOWN)&&y<878)
        {
            /*y+=2;
            this.repaint();*/
            if (forward == NULL)
            {
                forward = DOWN;
            }
        }
        else
        {
            forward = NULL;
        }
    }
    
    public void keyReleased(KeyEvent e) 
    {
        if((e.getKeyCode()==KeyEvent.VK_A||e.getKeyCode()==KeyEvent.VK_LEFT)&&x>-10)
        {
            /*x-=2;
            this.repaint();*/
            if (forward == LEFT)
            {
                forward = NULL;
            }
        }
        else if((e.getKeyCode()==KeyEvent.VK_D||e.getKeyCode()==KeyEvent.VK_RIGHT)&&x<878)
        {
            /*x+=2;
            this.repaint();*/
            if (forward == RIGHT)
            {
                forward = NULL;
            }
        }
        else if((e.getKeyCode()==KeyEvent.VK_W||e.getKeyCode()==KeyEvent.VK_UP)&&y>-10)
        {
            /*y-=2;
            this.repaint();*/
            if (forward == UP)
            {
                forward = NULL;
            }
        }
        else if((e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_DOWN)&&y<878)
        {
            /*y+=2;
            this.repaint();*/
            if (forward == DOWN)
            {
                forward = NULL;
            }
        }
    }
    
    public void run()
    {
        while (true)
        {
            switch (forward)
            {
            case UP: 
                y -= 2;
                System.out.println("UP");
                break;
            case DOWN:
                y += 2;
                System.out.println("DOWN");
                break;
            case LEFT:
                x -= 2;
                System.out.println("LEFT");
                break;
            case RIGHT:
                x += 2;
                System.out.println("RIGHT");
                break;
            }
            this.repaint();
            try 
            {
                Thread.sleep(50);
            }
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        }
    }
}