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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杜光 高级黑马   /  2013-6-9 22:32  /  1514 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杜光 于 2013-6-10 07:40 编辑

运行报错


AwtDemo.java:50: 错误: <匿名AwtDemo$3>不是抽象的, 并且未覆盖MouseListener中的抽
象方法mouseExited(MouseEvent)
                {
                ^
1 个错误
  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.                 })


  49.                 f.setVisible(true);
  50.         }
  51. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

2 个回复

正序浏览
楼主没有实现MouseListener的抽象方法
  1. import java.awt.Button;
  2. import java.awt.FlowLayout;
  3. import java.awt.Frame;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;

  10. public class  AwtDemo
  11. {
  12.         public static void main(String[] args)
  13.         {
  14.                 Frame f = new Frame("my awt");
  15.                 f.setSize(500,400);
  16.                 f.setLocation(300,200);
  17.                 f.setLayout(new FlowLayout());

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

  19.                 f.add(but);

  20.                 f.addWindowListener(new WindowAdapter()
  21.                 {
  22.                         public void windowClosing(WindowEvent e)
  23.                         {
  24.                                 System.out.println("我关");
  25.                                 System.exit(0);
  26.                         }

  27.                         public void windowActivated(WindowEvent e)
  28.                         {
  29.                                 System.out.println("我被激活了");
  30.                         }

  31.                         public void windowOpened(WindowEvent e)
  32.                         {
  33.                                 System.out.println("我被打开了,哈哈哈");
  34.                         }
  35.                 });
  36.                

  37.                 //按键监听器
  38.                 but.addActionListener(new ActionListener ()
  39.                 {
  40.                         public void actionPerformed(ActionEvent e)
  41.                         {
  42.                                 System.out.println("退出  按钮干的!");
  43.                                 System.exit(0);
  44.                         }
  45.                 });

  46.                 //鼠标监听器
  47.                
  48.                 but.addMouseListener(new MouseListener()
  49.                 {
  50.                         
  51.                         public void mouseEntered(MouseEvent e)
  52.                         {
  53.                                 System.out.println("你碰到我啦!");

  54.                         }
  55.                         //以下四个方法时        MouseLinstner的抽象方法
  56.                                                 @Override
  57.                                                 public void mouseClicked(MouseEvent e) {
  58.                                                        
  59.                                                        
  60.                                                 }

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

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

  71.                                                 @Override
  72.                                                 public void mouseExited(MouseEvent e) {
  73.                                                        
  74.                                                        
  75.                                                 }

  76.                                                
  77.                                        
  78.                 }) ;
  79.                

  80.                 f.setVisible(true);
  81.         }
  82. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
重写MouseListener中的抽象方法mouseExited(MouseEvent)就可以啊
最简单的就是只将MouseListener中的抽象方法加上,不写方法体
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马