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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胡斌 中级黑马   /  2012-10-3 23:24  /  1603 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Test {
        public static void main(String[] args)
        {
                ButtonDemo myButton=new ButtonDemo();
                myButton.setVisible(true);
               
        }
}
class ButtonDemo extends JFrame implements ActionListener{
        public static final int width=250;
        public static final int height=200;
        ButtonDemo()
        {
                setSize(width,height);
                setTitle("事件驱动祥例");
                Container conPane=getContentPane();
                conPane.setBackground(Color.blue);
                conPane.setLayout(new FlowLayout());
                JButton redB=new JButton("red");
                redB.addActionListener(this);//问题:当注册监视器后,点击按钮,系统是怎样调用类中的方法来处理事件的,例如,系统有一个内设handleEvent(Event ev)方法,系统是怎样通过这个方法联系下面actionPerformed(ActionEvent e)方法的?传递的是什么参数?
                conPane.add(redB);
                JButton greenB=new JButton("green");
                greenB.addActionListener(this);
                conPane.add(greenB);
               
        }
        public void actionPerformed(ActionEvent e)
        {
                Container conPane=getContentPane();
                if(e.getActionCommand().equals("red"))
                {
                        conPane.setBackground(Color.red);
                       
                }
                else if(e.getActionCommand().equals("green"))
                {
                        conPane.setBackground(Color.green);
                }
               
               
        }
}

比如这个程序:当点击red按钮时背景变为红色。我的问题是:点击时,发生点击事件,然后这个事件参数传递到那去了,然后又通过什么函数联系actionPerformed(ActionEvent e)函数的,这个接受的什么参数?

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
过几天我们就学到了,,,学完再告诉你哈
回复 使用道具 举报
用到了观察者模式~

在JButton的父类AbstractButton中我找到了这样的代码

protected void fireActionPerformed(ActionEvent event) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        ActionEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners==ActionListener.class) {
                // Lazily create the event:
                if (e == null) {
                      String actionCommand = event.getActionCommand();
                      if(actionCommand == null) {
                         actionCommand = getActionCommand();
                      }
                      e = new ActionEvent(AbstractButton.this,
                                          ActionEvent.ACTION_PERFORMED,
                                          actionCommand,
                                          event.getWhen(),
                                          event.getModifiers());
                }
                ((ActionListener)listeners[i+1]).actionPerformed(e);//这里遍历了所有的监听
            }         
        }

我的猜测,传入的参数 event应该就是鼠标点击的事件 ,这个fireActionPerformed 方法就是点击后调用的~

具体的调用我Debug了下,发现调用还是比较复杂的~我对Gui不了解,具体怎么触发的鼠标点击事件,没有耐心去找了。。。






评分

参与人数 2技术分 +1 黑马币 +10 收起 理由
唐志兵 + 1 赞一个!
胡斌 + 10 赞一个!

查看全部评分

回复 使用道具 举报
这个据说是要用到系统内设函数handleEvent(event ev)这个函数。但不知具体怎么传参?流程?
回复 使用道具 举报
啦啦啦,马上升级金牌黑马~~ 是帖我就顶~~  
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马