SWT学习笔记 第一节 shell

转载 2013年12月06日 11:58:24

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

swt shell 样式

shell SWT.BORDER //建立一个有边框但没有标题栏的窗口 SWT.CLOSE //建立一个只有关闭按钮的窗口 SWT.MIN  //建立一个不能最大化的窗口 SWT.MAX, /...
  • zhangxu365
  • zhangxu365
  • 2012年06月19日 15:20
  • 1247

SWT中Shell中的属性设置

BORDER——当只有BORDER的时候,窗口是一个只有细细白色边框的空白窗口,没有title那一圈蓝色的边框,也没有最大化,最小化,关闭。不能resize,不能移动。在任务栏里右键没有反应。 ...
  • u011208342
  • u011208342
  • 2014年04月29日 13:24
  • 1156

SWT的SHELL样式

Shell dlgShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);  所谓模态对话框,就是指除非采取有效...
  • lydia88
  • lydia88
  • 2016年09月28日 14:39
  • 702

SWT中的Display 对象和 Shell对象

我们书写swt程序的步骤,这些步骤是: 1.      创建一个Display对象 2.      创建一个或者多个Shell对象,你可以认为Shell代表了程序的窗口。 3.   ...
  • orange2211
  • orange2211
  • 2015年03月26日 10:02
  • 1140

[转]swt 全屏

[转]http://nricheton.homeip.net/?p=3The best way to achieve this is to open a shell and fill the scre...
  • rf_wu
  • rf_wu
  • 2007年08月25日 15:29
  • 4153

SWT Shell容器支持的5种事件

Shell容器支持的五种事件分别是:Activate , Close , Deactivate , Deiconfiy , Uconify; Activate:窗户活动时候触发。创建窗口,或者焦...
  • lenovo96166
  • lenovo96166
  • 2016年09月21日 13:24
  • 1252

SWT应用程序切换Shell中的Composite

为了这东西,搞的我头都疼了,找了好多书,看了很多贴子,都没说这个的。后来还是不小心弄出来的。 下面的一个例子,有一个Shell和两个Composite组成,每个Composite中各方一个But...
  • QQzyb
  • QQzyb
  • 2013年07月05日 17:31
  • 1325

SWT实现透明背景代码

import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.FillLayou...
  • h1987983372
  • h1987983372
  • 2011年12月29日 10:41
  • 2933

一个SWT应用程序窗口最大化最小化问题?

swt的window是有 shell控制的,因此改变它的相关属性即可 Shell shell = new Shell(SWT.MIN | SWT.CLOSE | SWT.MAX); 这样就可...
  • szyyyp
  • szyyyp
  • 2015年01月08日 15:12
  • 2156

SWT/Jface Shell、对话框的模态化

在我们开发UI时,有时有这样的需求,就是在弹出对话框时, 我们只能操作当前的对话框,只有选择合适的数据,关闭此对话框时,我们才能操作下一层的UI。 好歹 这个不用我们去写麻烦的代码,SWT 给我们...
  • soszou
  • soszou
  • 2012年08月28日 18:54
  • 3108
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SWT学习笔记 第一节 shell
举报原因:
原因补充:

(最多只允许输入30个字)