A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杜光 高级黑马   /  2013-6-10 07:48  /  1585 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杜光 于 2013-6-11 09:47 编辑

第一段鼠标监听器里覆盖了MouseListener中的抽象方法,但是第二段没有覆盖父类的方法,怎么也能执行顺利而不会报错呢??
  1. import java.awt.*;
  2. import java.awt.event.*;

  3. class  AwtDemo
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 Frame f = new Frame("my awt");
  8.                 f.setSize(500,400);
  9.                 f.setLocation(300,200);
  10.                 f.setLayout(new FlowLayout());

  11.                 Button but = new Button("我是一个按钮");

  12.                 f.add(but);

  13.                 f.addWindowListener(new WindowAdapter()
  14.                 {
  15.                         public void windowClosing(WindowEvent e)
  16.                         {
  17.                                 System.out.println("我关");
  18.                                 System.exit(0);
  19.                         }

  20.                         public void windowActivated(WindowEvent e)
  21.                         {
  22.                                 System.out.println("我被激活了");
  23.                         }

  24.                         public void windowOpened(WindowEvent e)
  25.                         {
  26.                                 System.out.println("我被打开了,哈哈哈");
  27.                         }
  28.                 });
  29.                

  30.                 //按键监听器
  31.                 but.addActionListener(new ActionListener ()
  32.                 {
  33.                         public void actionPerformed(ActionEvent e)
  34.                         {
  35.                                 System.out.println("退出  按钮干的!");
  36.                                 System.exit(0);
  37.                         }
  38.                 });

  39.                 //鼠标监听器
  40.                
  41.                 but.addMouseListener(new MouseListener()
  42.                 {
  43.                         
  44.                         public void mouseEntered(MouseEvent e)
  45.                         {
  46.                                 System.out.println("你碰到我啦!");

  47.                         }

  48.                         @Override
  49.                         public void mouseClicked(MouseEvent e)
  50.                         {
  51.                                                         
  52.                                                         
  53.             }

  54.            @Override
  55.             public void mousePressed(MouseEvent e)
  56.                         {
  57.                                                         
  58.                                                         
  59.             }

  60.             @Override
  61.              public void mouseReleased(MouseEvent e)
  62.                         {
  63.                                                         
  64.                                                         
  65.             }

  66.              @Override
  67.             public void mouseExited(MouseEvent e)
  68.                         {
  69.                                                         
  70.                                                         
  71.             }

  72.                 });


  73.                 f.setVisible(true);
  74.         }
  75. }
复制代码
第二段代码:
  1. import java.awt.*;
  2. import java.awt.event.*;

  3. class  MouseAndKeyboard
  4. {
  5.         //定义该图形中所需的组件引用
  6.         private Frame f;
  7.         private Button but;

  8.         MouseAndKeyboard()
  9.         {
  10.                 init();
  11.         }
  12.         
  13.         public void init()
  14.         {
  15.                 f = new Frame("My Frame");

  16.                 //对frame进行基本设置
  17.                 f.setBounds(300,100,500,400);
  18.                 f.setLayout(new FlowLayout());

  19.                 but = new Button("My Button");

  20.                 //将组件添加到frame中
  21.                 f.add(but);

  22.                 //加载一下窗体上事件
  23.                 myEvent();

  24.                 //现实窗体
  25.                 f.setVisible(true);

  26.                
  27.         }

  28.         private void myEvent()
  29.         {
  30.                 f.addWindowListener(new WindowAdapter()
  31.                 {
  32.                         public void windowClosing(WindowEvent e)
  33.                         {
  34.                                 System.exit(0);
  35.                         }
  36.                 });

  37.                 //添加一个按钮具备退出的功能
  38.                 //按钮就是事件源,选择哪个监听器?
  39.                 //通过关闭窗体示例,知道哪个组件具备什么样的特有监听器,需要查看该组件对象的功能
  40.                 //通过查阅button的API描述,发现按钮支持一个特有监听 addActionListener.

  41.                 but.addActionListener(new ActionListener()
  42.                 {
  43.                         public void actionPerformed(ActionEvent e)
  44.                         {
  45.                                 System.out.println("不和你玩了,按钮干的");
  46.                                 System.exit(0);
  47.                         }
  48.                 });

  49.                 but.addMouseListener(new MouseAdapter()
  50.                 {
  51.                         private int count = 1;
  52.                         private int click = 1;
  53.                         private int click2 = 1;
  54.                         public void mouseEntered(MouseEvent e)
  55.                         {

  56.                                 System.out.println("碰我"+(count++)+"次啦!!!");
  57.                         }

  58.                         public void mouseClicked(MouseEvent e)
  59.                         {
  60.                                 if (e.getClickCount()==2)
  61.                                 {
  62.                                         System.out.println("你连点了我两下"+click2++);
  63.                                 }
  64.                                 System.out.println("你点到我了"+click++);
  65.                         }
  66.                 });
  67.         }

  68.         public static void main(String[] args)
  69.         {
  70.                 new MouseAndKeyboard();
  71.         }
  72. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
夜默 + 1

查看全部评分

4 个回复

倒序浏览
学习学习!
回复 使用道具 举报
MouseAdapter是一个适配器,它是实现了MouseListener的一个抽象类,但是里面根本就没有抽象方法。所以你不重写父类方法也不会报错。而第一段代码中的MouseListener是一个接口,必须实现它的方法。给你看看MouseAdapter的代码吧:
  1. public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener {
  2.     /**
  3.      * {@inheritDoc}
  4.      */
  5.     public void mouseClicked(MouseEvent e) {}

  6.     /**
  7.      * {@inheritDoc}
  8.      */
  9.     public void mousePressed(MouseEvent e) {}

  10.     /**
  11.      * {@inheritDoc}
  12.      */
  13.     public void mouseReleased(MouseEvent e) {}

  14.     /**
  15.      * {@inheritDoc}
  16.      */
  17.     public void mouseEntered(MouseEvent e) {}

  18.     /**
  19.      * {@inheritDoc}
  20.      */
  21.     public void mouseExited(MouseEvent e) {}

  22.     /**
  23.      * {@inheritDoc}
  24.      * @since 1.6
  25.      */
  26.     public void mouseWheelMoved(MouseWheelEvent e){}

  27.     /**
  28.      * {@inheritDoc}
  29.      * @since 1.6
  30.      */
  31.     public void mouseDragged(MouseEvent e){}

  32.     /**
  33.      * {@inheritDoc}
  34.      * @since 1.6
  35.      */
  36.     public void mouseMoved(MouseEvent e){}
  37. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
夜默 + 1

查看全部评分

回复 使用道具 举报
武志红 发表于 2013-6-10 11:09
MouseAdapter是一个适配器,它是实现了MouseListener的一个抽象类,但是里面根本就没有抽象方法。所以你不 ...

太谢谢你了
回复 使用道具 举报
楼主你好  如果帖子的问题已经解决,请把帖子的类型改为“已解决”。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马