2013-10-12 76 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