为什么键侦听器不起作用?

亚特兰蒂斯

我正在尝试运行为移动的球编写的代码(对不起,如果代码太乱了...我不是很有经验...)它没有显示任何错误消息,但是当我单击appletviewer时,按下键,球不会改变方向。为什么会这样呢?ps我正在使用“ eclipse”编写代码,这是一个好的编译器吗?也许问题在那里?

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;

public class Main extends Applet implements KeyListener {

    private static final long serialVersionUID = 7526472295622776147L;

    boolean right=true;
    boolean left=false;
    boolean up=false;
    boolean down=false;
    boolean inGame=true;

    public void listen(){
        addKeyListener((KeyListener) this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void keyPressed(KeyEvent e){}

    public void keyTyped(KeyEvent e){
        int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        left=true;
        up=false;
        down=false;
    }

    if (key == KeyEvent.VK_RIGHT) {
        right=true;
        up=false;
        down=false;
    }

    if (key == KeyEvent.VK_UP) {
        up=true;
        right=false;
        left=false;
    }

    if (key == KeyEvent.VK_DOWN) {
        down=true;
        right=false;
        left=false;
    }

}
    public void keyReleased(KeyEvent e){}
    int x1=5;
    int y1=5;
    int x2=x1+5;
    int y2=y1+5;    

    public int moveRight(){
        return ++x1;
    }

    public int moveLeft(){
        return --x1;
    }

    public int moveUp(){
        return ++y1;
    }

    public int moveDown(){
        return --y1;
    }

    public void paint1(Graphics g){
        g.drawOval(x1,y1,x2,y2);
    }

    public void paint(Graphics e){
        long millis =System.currentTimeMillis();
        long millisn =System.currentTimeMillis();           
        while (right=true){
            millis =System.currentTimeMillis();
            millisn =System.currentTimeMillis();

            while (millisn<millis+20){
                millisn=System.currentTimeMillis();
            }    
        e.setColor(Color.white);
        e.drawOval(x1,y1,x2,y2);
        e.setColor(Color.red);
        moveRight();
        e.drawOval(x1,y1,x2,y2);
        }
        while(inGame==true){
            if(right==true){                    
                millis =System.currentTimeMillis();
                millisn =System.currentTimeMillis();
                    while (millisn<millis+20){
                        millisn=System.currentTimeMillis();
                    }    
                e.setColor(Color.white);
                e.drawOval(x1,y1,x2,y2);
                e.setColor(Color.red);
                moveRight();
                e.drawOval(x1,y1,x2,y2);
                listen();
            }    
            else if(down==true){
                    millis =System.currentTimeMillis();
                    millisn =System.currentTimeMillis();                        
                        while (millisn<millis+20){
                                    millisn=System.currentTimeMillis();
                        }    
                    e.setColor(Color.white);
                    e.drawOval(x1,y1,x2,y2);
                    e.setColor(Color.red);
                    moveDown();
                    e.drawOval(x1,y1,x2,y2);
            }
                else if (left==true){
                    millis =System.currentTimeMillis();
                    millisn =System.currentTimeMillis();    
                        while (millisn<millis+20){
                            millisn=System.currentTimeMillis();
                        }    
                    e.setColor(Color.white);
                    e.drawOval(x1,y1,x2,y2);
                    e.setColor(Color.red);
                    moveLeft();
                    e.drawOval(x1,y1,x2,y2); 
         }}
    }
}
疯狂程序员

问题很可能是,小程序没有键盘焦点。这是KeyListener的常见问题。

虽然您已将applet设置为可聚焦,但这并不意味着applet具有键盘焦点。

您可以尝试使用requestFocusInWindow,但这可能无法在applet中按预期方式工作。您还可以向小程序中添加一个MouseListener,以便当用户单击小程序时,您将requestFocusInWindow确保小程序具有键盘焦点。

我建议,如果您必须开发一个applet,请尝试使用JApplet建议您不要使用直接绘制到applet本身的方法,而是建议您使用自定义组件,使用诸如,之类的名称JPanel,并改写其paintComponent方法。

除了在组件部署方面提供灵活性之外,它还具有双重缓冲功能。

别忘了打电话 super.paintXxx

此外,这还将允许您使用键绑定API,该API可以克服 KeyListener

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章