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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 15383016390 中级黑马   /  2015-6-26 09:25  /  2591 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

3黑马币
我给按钮添加鼠标监听器,可就是没有效果,还提示鼠标事件无效。下面是我的代码。
public class MouseEvent {
        private Frame f;
        private Button b;
        MouseEvent(){
                init();
        }
        public void init(){
                f=new Frame("myFrame");
                f.setBounds(400, 400, 600, 800);
                f.setLayout(new FlowLayout());
                f.setBackground(new Color(0,100,0));
                b=new Button("Mybutton");
                f.add(b);
                myEvent();
                f.setVisible(true);
        }
        private void myEvent(){
                f.addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent e){
                                System.out.println("我关了");
                                System.exit(0);
                        }
                });
                b.addActionListener(new ActionListener(){
                        int a=0;
                        public void actionPerformed(ActionEvent e) {
                                System.out.println("Action"+a++);                               
                        }
                       
                });
                b.addMouseListener(new MouseAdapter(){
                        int c;
                        public void mouseEntered(MouseEvent e){
                                System.out.println("enter"+c++);
                        }
                });
        }

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                new MouseEvent();
        }


}


7 个回复

倒序浏览
C:\Users\Administrator\Desktop\tupian\1122.jpg

点评

呀,图片怎么显示不出来!反正就是public class MouseEvent 中的MouseEvent 类名出错了,改一下,然后将构造函数和调用对象的名称都相应的改一下就OK。  发表于 2015-6-26 10:19
回复 使用道具 举报
你的类名出错了。是关键字。改一下再运行一遍就可以了
回复 使用道具 举报
改一下类名
回复 使用道具 举报
要不就是没有导入功能
回复 使用道具 举报
zhangshuai_123 发表于 2015-7-2 11:21
要不就是没有导入功能

就是类名写成了关键字。
回复 使用道具 举报
类名错了么?我没看太懂!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马