• 第三章 第四节 剖析Display对象


    第四节          剖析Display对象

    返回目录

     

    Display对象是程序级别的SWT类与底层视窗系统实现之间的连接。Display类是依赖与视窗系统的,在某些平台上可能还会有一些附加的API函数。这里,我们只讨论广泛适用的那部分API

    通常,程序有一个,仅有一个Display对象(这是某些视窗系统底层的限制)。默认地,创建display对象的那个线程,就是执行事件循环、作为用户接口的线程。调用控件的大多数成员函数,只能通过用户接口线程。其它的线程访问这些成员(函数),会产生一个SWT.ERROR_THREAD_INVALID_ACCESS类型的异常。

    这个类的最主要的任务就是它的事件处理机制。Display类维护了一组注册的事件监听者(event listener),从底层视窗系统的事件队列中读取事件,然后传递给合适的逻辑单元去处理。

    SWT的事件处理机制中有两个层次。在底层,Listener通过display对象注册,用标识符指定与其相关联的事件的类型。当相关联的事件发生的时候,ListenerhandleEvent()函数被调用。这个系统不像其它的那样文雅端庄,但它更高效。

    在高层,通知一个事件的出现采用了“typed”的实现方式。那些注册并被用来监听这些事件的类都实现了EventListener的子接口[1]。这个系统更文雅、具有粒度性、面向对象,但对系统开销的要求也高。

    典型地,创建一个Display对象不需要参数;但也可以从DeviceData对象构建,这在调试时会比较有用。表3-1描述了Display的构造函数。

    构造函数

    说明原文

    说明

    public Display()

    Creates a new Display object and sets the current thread to be the user-interface thread. You'll almost always use either this constructor or Display.getDefault() in your application.

    创建一个新的display对象,并将当前线程设为用户接口线程。绝大多数时候都会用这个构造函数或Display.getDefault()

    public Display(DeviceData data)

    Creates a new Display object, setting the DeviceData member of the Display. You use DeviceData for some lower-level debugging and error configuration.

    创建一个新的Display对象,并把displayDeviceData设为传入的值。使用DeviceData可以作底层的调试和纠错。

    3-1  Display的构造函数

    Display还有一些函数,其中一些可被忽略(beep())。表3-2列出了Display的函数。

    函数

    说明原文

    说明

    void addFilter(int eventType, Listener listener)

    Adds a listener that's notified when an event of the type specified by eventType occurs.

    添加一个listener,当eventType定义的事件发生时,会通知listener去处理

    void addListener(int eventType, Listener listener)

    Adds a listener that's notified when an event of the type specified by eventType occurs.

    添加一个listener,当eventType定义的事件发生时,会通知listener去处理

    void asyncExec(Runnable runnable)

    Gives non-user-interface threads the ability to invoke the protected functions of the SWT widget classes. The user-interface thread performs the code (invokes the run() method) of the runnable at its next "reasonable opportunity." This function returns immediately. See syncExec().

    允许非用户接口的线程调用SWT控件类的protected函数。用户接口线程在下一次“合适的时机”运行runnable(调用run()方法)。这个函数会立即返回。请参见syncExec()

    void beep()

    Sounds a beep.

    蜂鸣(发出“嘟”的一声)

    void close()

    Closes this display.

    关闭display

    void disposeExec(Runnable runnable)

    Registers a Runnable object whose run() method is invoked when the display is disposed.

    注册一个Runnable对象,它的run() 方法在display被销毁的时候被调用。

    static Display findDisplay (Thread thread)

    Given a user-interface thread, this function returns the associated Display object. If the given thread isn't a user-interface thread, this method returns null.

    传递一个用户交换线程,这个函数返回与该线程关联的Display对象。如果传递的线程不是一个用户交互线程,这个方法将返回null

    Widget findWidget(int handle)

    Returns the widget for the specified handle, or null if no such widget exists.

    返回特定句柄(handle) 的控件;如果控件不存在则返回null

    Shell getActiveShell()

    Returns the currently active Shell, or null if no shell belonging to the currently running application is active.

    返回当前处于激活状态的Shell;如果没有属于当前运行的程序的shell处于激活状态 ,则返回null

    Rectangle getBounds()

    Returns this display's size and location.

    返回display的大小的位置

    Rectangle getClientArea()

    Returns the portion of this display that's capable of displaying data.

    返回display的用于显示数据的部分

    static Display getCurrent()

    If the currently running thread is a user-interface thread, this thread returns the Display object associated with the thread. If the thread isn't a privileged user-interface thread, this method returns null.

    如果当前运行的线程是用户交换线程,返回与这个线程关联的Display对象。如果线程不是用户交换线程,返回null

    Control getCursorControl()

    If the mouse or other pointing device is over a control that's part of the current application, this function returns a reference to the control; otherwise, it returns null.

    如果鼠标或其它指针设备在一个当前程序的控件上,返回那个控件的一个引用;否则,返回null

    Point getCursorLocation()

    Returns the location of the on-screen pointer relative to the top left corner of the screen.

    返回指针(鼠标点)所在的位置,以屏幕左上角作为原点。

    Point[] getCursorSizes()

    Returns the recommended cursor sizes.

    返回建议的指针大小。

    Object getData()

    Returns the application-specific data set into this display.

    display返回程序特定的数据集。

    Object getData(String key)

    Returns the application-specific data for the specified key set into this display.

    display返回由key指明的程序特定的数据

    static Display getDefault()

    Returns the default display of this application. If one hasn't yet been created, this method creates one and marks the current thread as the user-interface thread. The side effect of becoming the user-interface thread obligates the use of the current thread as the event loop thread for the application.

    返回程序的默认display。如果还没有创建display,该方法创建一个display并把当前线程标记为用户交互线程。变成用户交互线程的副作用是使当前线程成为事件循环的线程。

    int getDismissalAlignment()

    Returns the alignment for the default button in a dialog, either SWT.LEFT or SWT.RIGHT.

    返回对话框中按钮的默认对齐方式,SWT.LEFTSWT.RIGHT

    int getDoubleClickTime()

    Sets the maximum amount of time that can elapse between two mouse clicks for a double-click event to occur.

    设置双击的时间间隔,即两次单击的间隔比这个时间短就认为是双击。

    Control getFocusControl()

    Returns the control that currently has the focus of the application. If no application control has the focus, returns null.

    返回得到焦点的控件。如果没有控件处于焦点,则返回null

    int getIconDepth()

    Returns the depth of the icons on this display.

    返回display上的图标的深度

    Point[] getIconSizes()

    Returns the recommended icon sizes.

    返回推荐的图标大小

    Monitor[] getMonitors()

    Returns the monitors attached to this display.

    返回附着在display上的monitor

    Monitor getPrimaryMonitor()

    Returns the primary monitor for this display.

    返回display的主monitor

    Shell[] getShells()

    Returns an array of the active shells (windows) that are associated with this Display.

    返回与display关联的shell

    Thread getSyncThread()

    If the user-interface thread is executing code associated with a Runnable object that was registered via the syncExec() method, this function will return a reference to the thread that invoked syncExec() (the waiting thread). Otherwise, this function returns null.

    如果用户交互线程正在运行通过syncExec()方法注册的Runnable对象的代码,该函数返回由syncExce()(等待线程)调用的线程的引用。否则,返回null

    Color getSystemColor(int id)

    Returns the matching system color as defined in the SWT class. If no color is associated with id, this method returns the color black. Remember this is a system color—you shouldn't dispose it when you're finished with it.

    返回SWT类中定义的适用于系统的颜色。如果没有和id关联的颜色,则返回黑色。记着这是系统颜色——你不再用它的时候不能将它销毁。

    Font getSystemFont()

    Returns a reference to a system font (it shouldn't be disposed), which is appropriate to be used in the current environment. In general, widgets are created with the correct font for the type of component that they represent and you should rarely need to change this value to maintain the correct system appearance.

    返回可以用在当前环境的系统字体的引用(不能被注销)。一般而言,创建控件时就已经有了正确的字体,因此几乎不用改变字体以维持系统正确的外观。

    Thread getThread()

    Returns the user-interface thread of this Display. The thread that created this Display is the user-interface thread.

    返回用display的户交互线程。创建display的线程是用户交互线程。

    Point map(Control from, Control to, int x, int y)

    Maps the point specified by x, y from the from control's coordinate system to the to control's coordinate system.

    x, y定义的点从from控件的坐标系映射到to控件的坐标系。

    Rectangle map(Control from, Control to, int x, int y, int width, int height)

    Maps the rectangle specified by x, y, width, height from the from control's coordinate system to the to control's coordinate system.

    x, y, width, height定义的矩形从from控件的坐标系映射到to控件的坐标系。

    Point map(Control from, Control to, Point point)

    Maps the specified point from the from control's coordinate system to the to control's coordinate system.

    把点pointfrom控件的坐标系映射到to控件的坐标系。

    Point map(Control from, Control to, Rectangle rectangle)

    Maps the specified rectangle from the from control's coordinate system to the to control's coordinate system.

    把矩形recanglefrom控件的坐标系映射到to控件的坐标系。

    boolean readAndDispatch()

    This is the main event function of the SWT system. It reads events, one at a time, off the windowing system's event queue. After receiving the event, it invokes the appropriate methods on the listener objects that have registered interest in this event. If no events are on the event queue, readAndDispatch() executes any requests that might have been registered with this display via syncExec() or asyncExec(), notifying any syncExeced threads on completion of the request. This method returns true if there are more events to be processed, false otherwise. Returning false allows the calling thread to release CPU resources until there are more events for the system to process via the sleep() method.

    这是SWT系统的主要的事件函数。它从视窗系统的事件队列中读取事件,一次一个。接收事件后,它从已经注册的处理该事件的listener对象中选取一个合适的,并调用这个方法。如果事件队列中没有事件,readAndDispatch()执行任何由syncExec()asyncExec()注册的请求,通知任何syncExeced线程来完成请求。如果由更多的事件需要处理,该方法返回true,否则返回false。返回false允许线程通过sleep()方法释放CPU资源,直到有事件需要系统处理。

    void removeFilter(int eventType, Listener listener)

    Removes the specified listener from the notification list for the specified event type.

    从通知列表中把eventType定义的listener删除。

    void removeListener(int eventType, Listener listener)

    Removes the specified listener from the notification list for the specified event type.

    从通知列表中把eventType定义的listener删除。

    static void setAppName (String name)

    Sets the application name.

    设置程序名。

    void setCursorLocation (int x, int y)

    Moves the on-screen pointer to the specified location relative to the top left corner of the screen.

    把屏幕上的指针移动到由x, y定义的位置,屏幕左上角作为原点。

    void setCursorLocation (Point point)

    Moves the on-screen pointer to the specified location relative to the top left corner of the screen.

    把屏幕上的指针移动到由x, y定义的位置,屏幕左上角作为原点

    void setData(Object data)

    Sets the application-specific data.

    设置程序特定的数据。

    void setData(String key, Object data)

    Sets the application-specific data for the specified key.

    设置由key指明的程序特定的数据。

    void setSynchronizer (Synchronizer synchronizer)

    Sets the synchronizer for this display.

    设置displaysynchronizer

    boolean sleep()

    Allows the user-interface thread to relinquish its CPU time until it has more events to process or is awakened via another means; for example, wake(). This allows the system to process events much more efficiently, as the user-interface thread only consumes CPU resources when it has events to process.

    允许用户交互线程放弃它的CPU时间,直到有事件需要处理或它被其它方法唤醒,像wake()。因为用户交互线程只在有事件要处理时消耗CPU资源,所以系统可以更有效地处理事件。

    void syncExec(Runnable runnable)

    Like asyncExec(), this method gives non-userinterface threads the ability to invoke the protected functions of the SWT widget classes. The user-interface thread performs this code (invokes the run method) of runnable at its next "reasonable opportunity." This function returns after the run method of the Runnable object returns.

    asyncExec()相似,该方法使非用户线程可以调用SWT控件类的protected方法。用户接口线程在下一次“合适的时机”运行runnable(调用run()方法)。这个函数会立即返回。

    void timerExec(int milliseconds, Runnable runnable)

    Registers a Runnable object that the user-interface thread runs after the specified time has elapsed.

    注册一个Runnable对象,设定的时间到达时运行(其实就是定时器)。

    void update()

    Causes all pending paint requests to be processed.

     

    void wake()

    Wakes up the user-interface thread if it's in sleep(). Can be called by any thread.

    如果用户交互线程在sleep()中,唤醒它。可以在任何线程中调用。

    3-2  Display的函数[2]

    虽然Display对象搭建了GUI的基础,但是并不在屏幕上显示任何图形组件。事实上,Display它自身什么都不显示。还要创建一个Shell对象代表的窗口。这引出了下一节的话题——Shell


    [1] 译注:原文The classes that are registered to listen for these events implement subinterfaces of EventListener.

    [2] 注:像这样的函数说明在javadoc或help中有,以后不再翻译。

     

    返回目录

  • 相关阅读:
    浅谈flume
    浅谈storm
    浅谈zookeeper
    IntelliJ IDEA 使用教程
    浅谈spark
    添加本地jar包到maven仓库
    eclipse通过maven进行打编译
    pom.xml中添加远程仓库
    maven编译错误maven-assembly-plugin:2.2-beta-5:assembly (default-cli) on project
    最长上升子序列
  • 原文地址:https://www.cnblogs.com/ols/p/2173305.html
Copyright © 2020-2023  润新知