2016-04-21 140 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

感謝您解決問題的幫助。我很感激。 –