一段Java图形编程的程序,有错不知道哪里有问题。
这段程序在Eclipse里有错误,addMouseListener下面有红线,报错了。
不知道哪里错了。
错误信息:
The method addMouseListener(MouseListener) in the type Component is not applicable for the arguments (Monitor1)
class MyFrame extends Frame{
ArrayList<Point> points = null;
MyFrame(String s){
super(s);
points = new ArrayList<Point>();
setLayout(null);
setBounds(200,200,400,400);
this.setBackground(Color.darkGray);
setVisible(true);
this.addMouseListener(new Monitor()); 就是这里的addMouseListener下面有红线报错
}
public void paint(Graphics g){
Iterator <Point>i = points.iterator();
while(i.hasNext()){
Point p = (Point)i.next();
g.setColor(Color.cyan);
g.fillOval(p.x, p.y, 20, 20);
}
}
public void addPoint(Point p){
points.add(p);
}
}
class Monitor extends MouseAdapter{
public void mousePressed(MouseEvent e){
MyFrame f = (MyFrame)e.getSource();
f.addPoint(new Point(e.getX(),e.getY()));
f.repaint();
}
}
[最优解释]
new MyFrame("draw");
}
}
这个应该不会错吧。我纠结了好久了。
[其他解释]
谢谢楼上,吃一堑,长一智,受教了。