首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

SWT源码分析 (1)

2012-12-24 
SWT源码分析 (一)?阅读本文需要知道搭建swt的开发环境,对SWT有基本的了解,最好对Windows api也有所了解,还

SWT源码分析 (一)

?

阅读本文需要知道搭建swt的开发环境,对SWT有基本的了解,最好对Windows api也有所了解,还要稍微了解一下JNI。

?

?

HelloSWT是一个基本的SWT程序,当点击输入框的时候,会弹出一个MessageBox。

?

这个程序也是我们分析SWT运行的一个介入点。程序中为hello这个Text增加了鼠标事件的监听器,在MessageBox box = new MessageBox(shell);这一行加上一个断点,因为可以肯定,在点击鼠标时,会执行这段代码,可以通过断点观察出前后程序执行的顺序。当在Text上点击鼠标时,会在断点处暂停,此时运行的上下文为:
SWT源码分析 (1)

?

?

从这里就可以看出程序的执行过程了,先从main方法开始,HelloSWT的51行为?if (!display.readAndDispatch()):

?

boolean runDeferredEvents () {boolean run = false;/** Run deferred events.  This code is always* called in the Display's thread so it must* be re-enterant but need not be synchronized.*/while (eventQueue != null) {/* Take an event off the queue */Event event = eventQueue [0];if (event == null) break;int length = eventQueue.length;System.arraycopy (eventQueue, 1, eventQueue, 0, --length);eventQueue [length] = null;/* Run the event */Widget widget = event.widget;if (widget != null && !widget.isDisposed ()) {Widget item = event.item;if (item == null || !item.isDisposed ()) {run = true;widget.sendEvent (event);}}/** At this point, the event queue could* be null due to a recursive invocation* when running the event.*/}/* Clear the queue */eventQueue = null;return run;}

?作用是从SWT的 eventQuene中取出一个事件(事件是怎么放入到eventQueue中的呢?后面说),找到事件对应的控件,让这个控件去处理这个事件。

?

到现在分析到前面断点那个图的第三步,后面的过程会继续分析。

未完待续。

热点排行