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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值