请高手给指点迷津

shicial 2002-05-30 01:44:09
我写了以下代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class j642 extends JApplet // implements ActionListener
{
JPanel touchpanel=new JPanel();
JLabel touchlabel=new JLabel("Active region");
MyMouseListener mml=new MyMouseListener();
MyMouseMotionListener mmml=new MyMouseMotionListener();
public void init()
{
touchlabel.setHorizontalAlignment(SwingConstants.CENTER);
touchpanel.setBackground(Color.white);
touchpanel.add(touchlabel);
touchpanel.addMouseListener(mml);
touchpanel.addMouseMotionListener(mmml);
this.getContentPane().setLayout(new GridLayout(2,0));
this.getContentPane().add(touchpanel);
}

class MyMouseListener extends MouseAdapter
{
public void mouseEntered(MouseEvent e)
{
showStatus("mouse entered the active region");
}

public void mouseExited(MouseEvent e)
{
showStatus("mouse exited the active region");
}
}

class MyMouseMotionListener extends MouseAdapter
{
public void mouseMoved(MouseEvent e)
{
showStatus("mouse moved to location:x:"+e.getX()+"y:"+e.getY());
}

public void mouseDragged(MouseEvent e)
{
showStatus("mouse dragged to location;x;"+e.getX()+"y:"+e.getY());
}
}
}

编译出错:

j642.java [24:1] No method found matching addMouseMotionListener(MyMouseMotionListener)
touchpanel.addMouseMotionListener(mmml);
^
1 error

为什么Class MyMouseListener就没有问题,而类MyMouseMotionListener有问题呢?错误的意思是什么呀?请高手指点
...全文
145 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
robingranite 2002-05-30
  • 打赏
  • 举报
回复
因为addMouseMotionListener(MouseMotionListener l)要求的参数是MouseMotionListener,而你代码中的MyMouseMotionListener却是继承至MouseAdapter,所以在touchpanel.addMouseMotionListener(mmml)中出现了参数类型错误。
修改的方法是重写类MyMouseMotionListener的定义:
class MyMouseMotionListener extends MouseMotionAdapter

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧