黑马程序员技术交流社区

标题: 一个GUI的问题,求各位大侠帮助! [打印本页]

作者: 胡斌    时间: 2012-10-3 23:24
标题: 一个GUI的问题,求各位大侠帮助!
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)函数的,这个接受的什么参数?

作者: 黑马连家华    时间: 2012-10-3 23:46
过几天我们就学到了,,,学完再告诉你哈
作者: 黄小贝    时间: 2012-10-4 00:28
用到了观察者模式~

在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不了解,具体怎么触发的鼠标点击事件,没有耐心去找了。。。







作者: 胡斌    时间: 2012-10-4 10:04
这个据说是要用到系统内设函数handleEvent(event ev)这个函数。但不知具体怎么传参?流程?
作者: 夏天    时间: 2012-10-4 12:10
啦啦啦,马上升级金牌黑马~~ 是帖我就顶~~  




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2