SWT學習筆記 第一節 shell

來源:互聯網
上載者:User

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 :不可以最大化,最小化,不可以重設大小,可以關閉

 

顯示效果:

         

 

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( );    }}

 

 

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.設定 Shell Icon

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.