快樂的我正在使用----)這個項目希望我修改本章中的回彈程序,當單擊鼠標按鈕時動畫停止,當它再次單擊動畫時,恢復動畫。
當我用移動的笑臉點擊屏幕時,它不會停止,當我點擊它,也不會再次啓動,因爲我無法阻止笑臉移動我做錯了什麼?這是問題區.------)|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);
}
}
如果你不能得到你的程序開始,你如何能夠阻止它? 「你不能啓動也不能停止該計劃」是什麼意思?請詳細說明您採取了哪些步驟,以及在哪些地方出現錯誤,以及確切發生了哪些錯誤。根據需要編輯您的問題。 – planetmaker
當我用移動的笑臉點擊屏幕時。它不停止,當我點擊它,也沒有啓動,因爲我不能停止笑臉移動 –