为什么不KeyListener的工作吗? [英] Why doesn't the keylistener work?

查看:183
本文介绍了为什么不KeyListener的工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行code我写了一个移动的球(抱歉,如果code是凌乱...我不是很有经验......)它不显示任何错误消息,但是当我点击的的appletviewer和preSS的钥匙,球不改变它的方向。为什么会发生呢?
附:我使用月蚀写我的codeS是一个很好的编译器?也许这个问题是存在的?

 进口java.awt.event.KeyEvent中;
    进口java.awt.event.KeyListener;
    进口java.applet.Applet中;
    进口java.awt.Color中;
    进口java.awt.Graphics;公共类主要扩展小程序实现的KeyListener {    私有静态最后的serialVersionUID长= 7526472295622776147L;    布尔权= TRUE;
    布尔左= FALSE;
    布尔起来= FALSE;
    布尔下来= FALSE;
    布尔游戏中= TRUE;    公共无效听(){
        addKeyListener((的KeyListener)本);
        setFocusable(真);
        setFocusTraversalKeysEnabled(假);
    }    公共无效键pressed(KeyEvent的E){}    公共无效的keyTyped(KeyEvent的E){
        INT键= e.getKey code();    如果(键== KeyEvent.VK_LEFT){
        左= TRUE;
        UP = FALSE;
        向下= FALSE;
    }    如果(键== KeyEvent.VK_RIGHT){
        右=真;
        UP = FALSE;
        向下= FALSE;
    }    如果(键== KeyEvent.VK_UP){
        高达= TRUE;
        右= FALSE;
        左= FALSE;
    }    如果(键== KeyEvent.VK_DOWN){
        向下= TRUE;
        右= FALSE;
        左= FALSE;
    }}
    公共无效的keyReleased(KeyEvent的E){}
    INT X1 = 5;
    INT Y1 = 5;
    INT X2 = X1 + 5;
    INT Y2 = Y1 + 5;    公众诠释MoveRight的(){
        返回++ X1;
    }    公众诠释moveLeft(){
        返回--x1;
    }    公众诠释为moveUp(){
        返回++ Y1;
    }    公众诠释下移(){
        返回--y1;
    }    公共无效paint1(图形G){
        g.drawOval(X1,Y1,X2,Y2);
    }    公共无效漆(图形E){
        长米利斯= System.currentTimeMillis的();
        长millisn = System.currentTimeMillis的();
        而(右= TRUE){
            米利斯= System.currentTimeMillis的();
            millisn = System.currentTimeMillis的();            而(millisn<米利斯+ 20){
                millisn = System.currentTimeMillis的();
            }
        e.setColor(Color.white);
        e.drawOval(X1,Y1,X2,Y2);
        e.setColor(Color.red);
        向右移();
        e.drawOval(X1,Y1,X2,Y2);
        }
        而(游戏内== TRUE){
            如果(右==真){
                米利斯= System.currentTimeMillis的();
                millisn = System.currentTimeMillis的();
                    而(millisn<米利斯+ 20){
                        millisn = System.currentTimeMillis的();
                    }
                e.setColor(Color.white);
                e.drawOval(X1,Y1,X2,Y2);
                e.setColor(Color.red);
                向右移();
                e.drawOval(X1,Y1,X2,Y2);
                听();
            }
            否则,如果(下==真){
                    米利斯= System.currentTimeMillis的();
                    millisn = System.currentTimeMillis的();
                        而(millisn<米利斯+ 20){
                                    millisn = System.currentTimeMillis的();
                        }
                    e.setColor(Color.white);
                    e.drawOval(X1,Y1,X2,Y2);
                    e.setColor(Color.red);
                    下移();
                    e.drawOval(X1,Y1,X2,Y2);
            }
                否则,如果(左==真){
                    米利斯= System.currentTimeMillis的();
                    millisn = System.currentTimeMillis的();
                        而(millisn<米利斯+ 20){
                            millisn = System.currentTimeMillis的();
                        }
                    e.setColor(Color.white);
                    e.drawOval(X1,Y1,X2,Y2);
                    e.setColor(Color.red);
                    向左移动();
                    e.drawOval(X1,Y1,X2,Y2);
         }}
    }
}


解决方案

问题是,最有可能的,小程序不具有键盘焦点。这是keyListener的常见问题。

虽然已设置的小程序作为是聚焦的,但这并不意味着小程序具有键盘焦点。

您可以尝试使用 requestFocusInWindow ,但预计在applet,这可能无法正常工作。你还可以添加一个MouseListener的的小程序,以便当用户点击该小程序,你会 requestFocusInWindow ,以确保小程序具有键盘焦点

我会建议,相反,如果你要开发一个小程序,您尝试使用 JApplet的。而不是直接画到小程序本身,我建议你使用的自定义组件,这样说,的JP​​anel ,并覆盖其的paintComponent 方法来代替。

除了在问候组件的部署提供了灵活性,它也双缓冲

不要忘了叫 super.paintXxx

此外,这也将允许您使用具有克服许多的缺憾拍摄的能力键绑定API的的KeyListener

I'm trying to run a code I wrote for a moving ball (sorry if the code is messy... i'm not very experienced... ) it doesn't show any error messages but when I click at the appletviewer and press the keys, the ball doesn't change it's direction. Why does that happen? p.s. I'm using "eclipse" for writing my codes is it a good compiler? maybe the problem is there?

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

解决方案

The problem is, most likely, the applet doesn't have keyboard focus. This is a common issue with KeyListener.

While you have set the applet as being focusable, it doesn't mean that the applet has keyboard focus.

You could try using requestFocusInWindow, but this may not work as expected in applets. You could also add a MouseListener to the applet, so that when the user clicks on the applet, you would requestFocusInWindow to ensure that the applet has keyboard focus

I would recommend, instead, if you have to develop an applet, you try using JApplet. Instead of painting directly to the applet itself, I'd recommend that you use a custom component, say something like, JPanel, and override its paintComponent method instead.

Apart from providing flexibility in regards to the deployment of the component, it's also double buffered.

Don't forget to call super.paintXxx

Also, this would also allow you to use the key bindings API which has the ability to overcome many of the shot comings of the KeyListener

这篇关于为什么不KeyListener的工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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