关闭

SWT学习笔记 第一节 shell

193人阅读 评论(0) 收藏 举报
分类:

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 ofShellrepresent 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 calledSWT. The SWT class is located in

the org.eclipse.swt package. For shells, the enumerated values areBORDER ,CLOSE, MIN, MAX, NO_TRIM, RESIZE, andTITLE. Also,

two convenience values SHELL_TRIMandDIALOG_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 ofshell,

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_MODALandSWT.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 thestyle attributes desired to theDialogconstructor.

 

// 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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8326次
    • 积分:125
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:31篇
    • 译文:0篇
    • 评论:0条
    文章存档