簡體   English   中英

為什么鍵偵聽器不起作用?

[英]Why doesn't the keylistener work?

我正在嘗試運行為移動的球編寫的代碼(對不起,如果代碼太亂了……我不是很有經驗……)它沒有顯示任何錯誤消息,但是當我單擊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許多缺點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM