简体   繁体   中英

Why doesn't the keylistener work?

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? ps 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM