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控件知识学习笔记

  • 2013年06月11日 17:02
  • 55KB
  • 下载

SWT和JFACE学习笔记

  • 2008年10月10日 13:59
  • 70KB
  • 下载

[struts2学习笔记] 第一节 关于struts2的简单认知

本文就简单罗列了struts2开发中最最初级的东西,这些东西网上应该到处都有,自己感兴趣,简单罗列翻译了一下。...

Spring Mvc4.2学习笔记-第一节 spring mvc对比struts2优势

1、Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr...
  • nnsword
  • nnsword
  • 2016年05月05日 16:40
  • 453

七月算法(julyedu.com)5 月深度学习班学习笔记-第一节数学基础

关于数学在机器学习中的重要性: 程博是这样说的------ 不错,数学是算法的基础,没有好的数学根基是不能触碰到算法的根源,但是我认为一般非数学专业的人适可而止,不要太迷恋数学,我们大部分人,了解...
  • cd0601
  • cd0601
  • 2016年05月24日 22:31
  • 1542

HTML5学习笔记第一节(智能提示和视频音频标签)

简介 -------------------------------------------------------------------------------------------------...

SWT学习笔记!

SWT学习笔记! 1,SWT/JFace 与AWT/SWING  区别是?  java的标准图形 API : AWT/SWING 一直是被后人诟病的, 用它写的页面不够完美, 页面响应...

[shiro学习笔记]第一节 使用eclipse/myeclipse搭建一个shiro程序

初次使用shiro搭建开发环境,helloworld居然写了半天,爆出来了一大堆的错误信息,整个解决过程记录一下,成为一些宝贵的经验。...

七月算法深度学习 第三期 学习笔记-第一节 数学基础

深度学习数学基础: 需要掌握四板块数学: 1.微积分: 极限 微分与泰勒级数 积分与微积分基本定理 牛顿法 微分学的核心思想是逼近。 一元微分学顶峰-泰勒级数: 牛顿...

Linux Shell 的学习笔记

  • 2017年11月01日 11:17
  • 2.21MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SWT学习笔记 第一节 shell
举报原因:
原因补充:

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