1
我不能得到這個MouseListener
工作。爲什麼?當我點擊次鼠標MouseListener沒有迴應
import acm.program.*;
import acm.graphics.*;
import java.awt.event.*;
/** Draws an oval whenever the user clicks the mouse */
public class DrawOvals extends GraphicsProgram implements MouseListener {
public void run() {
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
GOval oval = new GOval(100,100,OVAL_SIZE, OVAL_SIZE);
oval.setFilled(true);
add(oval, e.getX(), e.getY());
System.out.println("Got here!");
}
/* Private constants */
private static final double OVAL_SIZE = 20;
/* implements the required methods for mouse listener*/
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
當你說這是行不通的,有什麼症狀? – Alvin 2012-03-14 00:47:54
mouseClicked方法是否被調用?嘗試在方法中添加System.out.println(「Got here!」)以查看。 – 2012-03-14 00:53:27
@羅伊。當我在圖形窗口上單擊鼠標時,沒有任何反應。我按照你的建議做了,也沒有打印在控制檯上,所以我猜mouseClicked方法沒有被調用。 – Dana 2012-03-14 01:02:04