我想在矩形外側單擊鼠標時更改背景顏色。我只是不知道如何使用MouseEvent。更改鼠標事件的背景顏色
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AnAppletWithMouseEvents extends Applet implements MouseListener {
public void init()
{
addMouseListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.drawRect(10, 30, 150, 150);
}
public void mouseClicked(MouseEvent e)
{
String clickDesc;
if (e.getClickCount() == 2)
clickDesc = "double";
else
clickDesc = "single";
System.out.println("Mouse was " + clickDesc + "-clicked at location (" +
e.getX() + ", " + e.getY() + ")");
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
那麼你的問題是什麼?當你點擊任何地方時,輸出是什麼? – Sibbo
究竟是什麼部分你有麻煩?你的'mouseClicked'方法工作嗎?如果是這樣,你有點鼠標點和矩形的邊界,你還需要什麼? – Kevin