2016-04-21 40 views
-1

快乐的我正在使用----)Happyface.gif这个项目希望我修改本章中的回弹程序,当单击鼠标按钮时动画停止,当它再次单击动画时,恢复动画。
当我用移动的笑脸点击屏幕时,它不会停止,当我点击它,也不会再次启动,因为我无法阻止笑脸移动我做错了什么?这是问题区.------)|ReboundPanel Inheritance

private class ReboundMouseListener implements MouseListener { 

    public void mouseClicked(MouseEvent event) { 
     if (timer.isRunning()) 
      timer.stop(); 
     else 
      timer.start(); 
     } 
    } 

    public void mouseEntered(MouseEvent event) {} 
    public void mouseExited(MouseEvent event) {} 
    public void mousePressed(MouseEvent event) {} 
    public void mouseReleased(MouseEvent event) {} 
} 

这里是代码的其余部分:

public class ReboundPanel extends JPanel { 
    private final int WIDTH =300, HEIGHT= 100; 
    private final int DELAY= 20, IMAGE_SIZE=35; 

    private ImageIcon image; 
    private Timer timer; 
    private int x, y, moveX, moveY; 

    //--------------------------------------------------------- 
    // Sets up the panel,including the timer for the animation. 
    //--------------------------------------------------------- 
    public ReboundPanel(){ 
     timer= new Timer(DELAY, new ReboundListener()); 
     image= new ImageIcon("happyFace.gif"); 

     x=0; 
     y=40; 
     moveX=moveY=3; 

     setPreferredSize(new Dimension(WIDTH, HEIGHT)); 
     setBackground(Color.black); 
     timer.start(); 
    } 

    //--------------------------------------------------------- 
    // Draws the image in the current location. 
    //--------------------------------------------------------- 
    public void paintComponent(Graphics page) 
    { 
     super.paintComponent(page); 
     image.paintIcon(this, page, x, y); 
    } 

    //********************************************************* 
    // Represents the action listener for the timer. 
    //********************************************************* 
    private class ReboundListener implements ActionListener 
    { 
     //-------------------------------------------------------- 
     // Updates the position of the image and possibly the direction 
     // of movement whenever the timer fires an action event. 
     //-------------------------------------------------------- 
     public void actionPerformed(ActionEvent event) 
     { 
      x += moveX; 
      y += moveY; 

      if (x <=0 || x >= WIDTH-IMAGE_SIZE) 
       moveX =moveX * -1; 

      if (y <=0 || y >= HEIGHT-IMAGE_SIZE) 
       moveY = moveY * -1; 

      repaint();  
     } 
    } 

    private class ReboundMouseListener implements MouseListener { 
     //-------------------------------------------------------------- 
     // Stops or starts the timer (and therefore the animation) 
     // when the mouse button is clicked. 
     //-------------------------------------------------------------- 
     public void mouseClicked(MouseEvent event) { 
      if (timer.isRunning()) 
       timer.stop(); 
      else 
       timer.start(); 
     } 

     //-------------------------------------------------------------- 
     // Provide empty definitions for unused event methods. 
     //-------------------------------------------------------------- 
     public void mouseEntered(MouseEvent event) {} 
     public void mouseExited(MouseEvent event) {} 
     public void mousePressed(MouseEvent event) {} 
     public void mouseReleased(MouseEvent event) {} 
    } 
} 

public class Rebound { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame("Rebound"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new ReboundPanel()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+1

如果你不能得到你的程序开始,你如何能够阻止它? “你不能启动也不能停止该计划”是什么意思?请详细说明您采取了哪些步骤,以及在哪些地方出现错误,以及确切发生了哪些错误。根据需要编辑您的问题。 – planetmaker

+0

当我用移动的笑脸点击屏幕时。它不停止,当我点击它,也没有启动,因为我不能停止笑脸移动 –

回答

2

看起来你缺少你addMouseListener()电话:

public ReboundPanel() { 
    // Other initializations ... 

    addMouseListener(new ReboundMouseListener()); // <-- add 

    timer.start(); 
} 
+0

当我添加代码它说:线程“主”java.lang.Error中的异常:错误:未解决的编译问题: \t ReboundMouseListener不能被解析为 –

+0

我认为你的'ReboundMouseListener'在你的'ReboundListener'类中,并且在格式化时被意外移出。让你的代码发生同样的“事故”,并找到'ReboundMouseListener'类。 – AJNeufeld

+0

感谢您解决问题的帮助。我很感激。 –