日期:2014-05-20  浏览次数:20832 次

一段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();
 
  }
 }
------最佳解决方案--------------------
引用:
import java.awt.*;
 import java.awt.event.*;
 import java.util.*;
 public class MouseAdapter {
  public static void main(String args[]){
  new MyFrame("draw");
  }
 
 }
这个应该不会错吧……

为什么总喜欢使用和jre提供的类同名的类名。

------其他解决方案--------------------
我copy 你的代码到eclipse没有错哈····是不是这里的代码不全哈,你的main方法呢?
------其他解决方案--------------------
Monitor 这个类是JVAVA的内置类,你换个类名把。
javax.management.monitor 
类 Monitor
java.lang.Object
  javax.management.NotificationBroadcasterSupport
      javax.management.monitor.Monitor
------其他解决方案--------------------
 class Monitor extends MouseAdapter{
   public void mousePressed(MouseEvent e){
   MyFrame f = (MyFrame)e.getSource();
   f.addPoint(new Point(e.getX(),e.getY()));
   f.repaint();
   
  }把这个放到类的外面就行,要不然是个内部类。。
------其他解决方案--------------------
我放到类的外面了啊。。。
------其他解决方案--------------------
我换了怎么还有问题啊。。。
------其他解决方案--------------------
还有什么问题?

------其他解决方案--------------------
应该不是前面的问题吧
------其他解决方案--------------------
不知道啊。。我把类名改了还是那里有问题。
------其他解决方案--------------------
import java.awt.*;
 import java.awt.event.*;
 import java.util.*;
 public class MouseAdapter {
  public static void main(String args[]){