zoukankan      html  css  js  c++  java
  • SWT学习笔记 第一节 shell

    1.Display 和 Shell

    We use two SWT classes to create windows: Display and Shell. Displayis the class responsible for managing the interaction between all SWT widgets

    and the underlying operating system. It is in Displaythat you find methods that enable you to directly query the operating system for information about

    things such as which control currently has the focus and what windows are currently open and attached to the display. You will not need to interact directly

    with the display very often.

    The second class, Shell, is much more important to the programmer. Instances of Shellrepresent windows which are currently being managed by the desktop

    (on MS Windows) or the windows manager (on Unix or Linux systems). Shells can be created either directly on the display, or within the confines of a parent shell.

    2. A very simple shell

    import org.eclipse.swt.widgets.*;
    
    public class SimpleShell {
        
         SimpleShell( )    {
            Display d = new Display( );
            Shell s = new Shell(d);
            s.setSize(500,500);
            s.open( );
            while(!s.isDisposed( )){
                if(!d.readAndDispatch( ))
                    d.sleep( );
            }
            d.dispose( );
        }
    }
     
    // A Runner class to execute examples
    public class Runner {
        
        public static void main(String[] args){
            SimpleShell ss = new SimpleShell( );        
        }
    }
    解释:
    The final code segment constitutes the shell's event loop:
    while(!s.isDisposed( )){
        if(!d.readAndDispatch( ))
          d.sleep( );
        }

    Shells respond to events , some fired by the operating system, others by user actions. Examples of events are things such as the user clicking the maximize

    button or closing the window created by the shell. The event loop continuously listens for these events and dispatches them to the appropriate handler

    (this is the purpose of the readAndDispatch() method of the Displayclass). This means that every shell you create must have this event loop.

    Failure to provide the event loop results in the shell being created, then immediately disposed of. The event loop continues to execute until

    the isDisposed( ) method of the Shellclass returns true, indicating that the window has been closed by the user.

    The last line releases the memory resources captured when you created the display:

    d.dispose( );

    It is important in SWT programming to remember to dispose of any widget you explicitly create. This prevents memory leaks.

    To specify a style for a shell, the SWT provides us with a set of enumerated values encapsulated in another class called SWT. The SWT class is located in

    the org.eclipse.swt package. For shells, the enumerated values are BORDER , CLOSE, MIN, MAX, NO_TRIM, RESIZE, and TITLE. Also,

    two convenience values SHELL_TRIM and DIALOG_TRIM combine several of the style attributes to create two common looks for windows.

    Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE);

    SHELL_TRIM:可以最大化,最小化,可以重置大小,可以关闭

    DIALOG_TRIM :不可以最大化,最小化,不可以重置大小,可以关闭

     

    显示效果:

    1          3

    3.Example . Opening ChildShell within a parent shell

    import org.eclipse.swt.widgets.*;
    
    public class ChildShellExample {
        Display d = new Display( );
        
        
        ChildShellExample( )    {    
            Shell s = new Shell(d);
            s.setSize(500,500);
            s.open( );
            ChildShell cs = new ChildShell(s);
            while(!s.isDisposed( )){
                if(!d.readAndDispatch( ))
                    d.sleep( );
            }
            d.dispose( );
        }
    }

    2

    4. Dialog

    The Dialog class enables you to create custom dialogsthose on which you can place any widget you desire. A dialog is simply another type of shell,

    except that it extends the Dialogclass, which encapsulates some additional methods and accepts additional style attributes.

    The Dialog class has two style attributes: SWT.APPLICATION_MODAL and SWT.SYSTEM_MODAL. APPLICATION_MODAL, as the name implies,

    will cause the dialog to halt all processing in the application until the dialog is dismissed. SYSTEM_MODALwill prevent the user from performing other tasks

    in any application running in the operating system while the dialog is open (although background tasks will still run on most platforms).

    As with Shell, you specify the dialog type at construction time by passing the style attributes desired to the Dialogconstructor.

    // Example An application modal dialog
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    
    public class DialogExample extends Dialog {    
        DialogExample(Shell parent)
        {
            super(parent);        
        }
        public String open( )
        {
            Shell parent = getParent( ); 
            Shell dialog = new Shell(parent, 
               SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 
            dialog.setSize(100,100);
            dialog.setText("A Dialog"); 
            dialog.open( ); 
            Display display = parent.getDisplay( ); 
            while (!dialog.isDisposed( )) 
            { if (!display.readAndDispatch( )) display.sleep( ); 
            } 
            return "After Dialog";
        }
    }
    // Example . Opening a dialog
    import org.eclipse.swt.widgets.*;
    
    public class ShellDialogExample {
        ShellDialogExample( )
        {
            Display d = new Display( );
            Shell s = new Shell(d);
            s.setSize(300,300);
            s.open( );
            DialogExample de = new DialogExample(s);
            String result = de.open( );
            System.out.println(result);
            while(!s.isDisposed( )){
                if(!d.readAndDispatch( ))
                    d.sleep( );
            }
            d.dispose( );
    
        }
    }

    显示效果: Dialog 只有结束了dialog对话框才可以使得原来的窗体继续执行!然而 childShell 不是,它不影响 parentShell 的运行

    4

    4.设定 Shell Icon

    shell.setImage(new Image(display, "C:\\java_coffee.ico"));

    5

  • 相关阅读:
    ngix反向代理-之反向
    redux和flux究竟有什么不同, 说点自己的理解
    npm发包记录
    由一个聚焦-focus-事件异常跟踪引起的总结
    git查看分支的几个方法
    test-your-mind-快速测试自己的代码
    contos7 yum安装php7.2与swoole (2)
    php_ thinkphp 时间回滚
    30个php操作redis常用方法代码例子
    redis_php 安装与卸载
  • 原文地址:https://www.cnblogs.com/yinger/p/2165280.html
Copyright © 2011-2022 走看看