SWT创建Shell窗口

通过不同的方式创建Shell窗口.(创建工程的时候需引入SWT包)
代码如下:

Java代码 复制代码
  1. import org.eclipse.swt.SWT;   
  2. import org.eclipse.swt.graphics.Image;   
  3. import org.eclipse.swt.widgets.Display;   
  4. import org.eclipse.swt.widgets.Shell;   
  5.   
  6. public class ShellTest {   
  7.     /*  
  8.      * 通过display来创建shell Shell(Display display)  
  9.      */  
  10.     public static void createShell1() {   
  11.         // 新建一个Display对象   
  12.         Display display = new Display();   
  13.         // 根据已创建的Display对象新建一个Shell对象   
  14.         // Shell的实例表示当前受Windows所管理的窗口   
  15.         Shell shell = new Shell(display);   
  16.         // 设置shell的大小(宽度和高度)   
  17.         shell.setSize(500500);   
  18.         // 设置shell的窗口名   
  19.         shell.setText("shell demo");   
  20.         // 打开shell窗口   
  21.         shell.open();   
  22.         // 控制窗口不关闭,如果没有下面的代码,窗口会立即自动关闭   
  23.         while (!shell.isDisposed()) {   
  24.             // 监听用户所引发的事件   
  25.             if (!display.readAndDispatch()) {   
  26.                 // 让窗口处于睡眠状态   
  27.                 display.sleep();   
  28.             }   
  29.         }   
  30.         // 当用户关闭窗口时,释放display占用的内存资源   
  31.         display.dispose();   
  32.     }   
  33.   
  34.     /*  
  35.      * 通过display和样式值来创建shell Shell(Display display[,int style])  
  36.      */  
  37.     public static void createShell2() {   
  38.         // 新建一个Display对象   
  39.         Display display = new Display();   
  40.         // 根据已创建的Display对象和样式值新建一个Shell对象   
  41.         // 可以通过|来组全不同的样式值来达到特定的效果   
  42.         Shell shell = new Shell(display, SWT.RESIZE | SWT.CLOSE   
  43.                 | SWT.BORDER_SOLID);   
  44.         // 设置shell的大小(宽度和高度)   
  45.         shell.setSize(500500);   
  46.         // 设置shell的窗口名   
  47.         shell.setText("shell demo");   
  48.         // 打开shell窗口   
  49.         shell.open();   
  50.         // 控制窗口不关闭,如果没有下面的代码,窗口会立即自动关闭   
  51.         while (!shell.isDisposed()) {   
  52.             // 监听用户所引发的事件   
  53.             if (!display.readAndDispatch()) {   
  54.                 // 让窗口处于睡眠状态   
  55.                 display.sleep();   
  56.             }   
  57.         }   
  58.         // 当用户关闭窗口时,释放display占用的内存资源   
  59.         display.dispose();   
  60.     }   
  61.   
  62.     /*  
  63.      * 在父Shell上创建子Shell Shell(Shell parent[,int tyle])  
  64.      */  
  65.     public static void createChildShell() {   
  66.         Display display = new Display();   
  67.         // 创建父Shell   
  68.         Shell parent = new Shell(display);   
  69.         parent.setSize(500500);   
  70.         parent.setText("parent");   
  71.         parent.open();   
  72.         // 在父Shell的基础之上创建几个子Shell   
  73.         Shell cs1 = new Shell(parent);   
  74.         cs1.setSize(250250);   
  75.         cs1.setText("child1");   
  76.         cs1.open();   
  77.   
  78.         Shell cs2 = new Shell(parent);   
  79.         cs2.setSize(250250);   
  80.         cs2.setText("child2");   
  81.         cs2.open();   
  82.   
  83.         Shell cs3 = new Shell(parent);   
  84.         cs3.setSize(250250);   
  85.         cs3.setText("child3");   
  86.         cs3.open();   
  87.   
  88.         while (!parent.isDisposed()) {   
  89.             if (!display.readAndDispatch()) {   
  90.                 display.sleep();   
  91.             }   
  92.         }   
  93.         display.dispose();   
  94.     }   
  95.   
  96.     /*  
  97.      * 创建模态窗口的对话框  
  98.      */  
  99.     public static void createDialog() {   
  100.         Display display = new Display();   
  101.         Shell parent = new Shell(display);   
  102.         parent.setSize(500500);   
  103.         parent.setText("parent");   
  104.         parent.setImage(new Image(display, "images/Info.png"));   
  105.         parent.open();   
  106.         // 通过SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL样式值来创建dialog   
  107.         Shell dialog = new Shell(parent, SWT.DIALOG_TRIM   
  108.                 | SWT.APPLICATION_MODAL);   
  109.         dialog.setSize(200200);   
  110.         dialog.setText("dialog");   
  111.         dialog.setImage(new Image(display, "images/Alert.png"));   
  112.         dialog.open();   
  113.   
  114.         while (!parent.isDisposed()) {   
  115.             if (display.readAndDispatch()) {   
  116.                 display.sleep();   
  117.             }   
  118.         }   
  119.         display.dispose();   
  120.     }   
  121.   
  122.     public static void main(String[] args) {   
  123.          ShellTest.createShell1();   
  124.          //ShellTest.createShell2();   
  125.          //ShellTest.createChildShell();   
  126.          //ShellTest.createDialog();   
  127.     }   
  128. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值