2013-10-12 78 views
1

我試圖運行我寫了一個移動的球一碼(對不起,如果代碼是凌亂...我不是很有經驗......)它不顯示任何錯誤消息,但是當我點擊的appletviewer並按下鍵,球不會改變它的方向。爲什麼會發生? p.s.我使用「eclipse」編寫我的代碼是一個好的編譯器嗎?也許問題在那裏?爲什麼Keylistener不工作?

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

拍攝缺憾的能力的關鍵綁定API大概是因爲沒有按小程序沒有鍵盤焦點。另外,不要忘記調用super.paint – MadProgrammer

+0

但我沒有設置可聚焦爲「true」 – Atlantis

+0

這僅僅意味着小程序可以接收鍵盤焦點,並不在於它具有鍵盤焦點 – MadProgrammer

回答

2

問題是,最有可能的是,小程序沒有鍵盤焦點。這是KeyListener常見的問題。

雖然已設置的小應用程序爲可聚焦的,這並不意味着該小程序具有鍵盤焦點。

你可以嘗試使用requestFocusInWindow,但預計將在applet,這可能無法正常工作。您還可以將一個MouseListener添加到該小程序中,以便當用戶單擊該小程序時,您將requestFocusInWindow確保該小程序具有鍵盤焦點

如果您必須開發小程序,嘗試使用JApplet。我建議您不要直接繪製applet本身,而是使用自定義組件,例如JPanel,然後替代其paintComponent方法。

除了在關於該組件的部署提供了靈活性,它也是雙緩衝。

不要忘記調用super.paintXxx

而且,這也將允許您使用的必須克服許多KeyListener

+0

抱歉,這可能無法正常工作,我不明白您對super.paintXxx – Atlantis

+0

最後一行如果覆蓋塗料,必須調用super.paint,如果你重寫paintComponent,你必須調用super.paintComponent ... – MadProgrammer