Swt/Jface中提供了各种各样的Dialog,一般情况下就能满足我们的需求,现在把这些dialog介绍一下,
下一篇文章再继续将如何自定义Dialog
一、MessageDialog
1,MessageDialog的用法很简单,只需要一句话.
![](http://hiphotos.baidu.com/frjay/pic/item/6e061d95f7ba2c367af48064.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/7bf40ad1631bdced562c8464.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/62d9f2d354ee3fe1a8ec9a64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/572c11dfac2ea42d62279864.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/632762d0fec1bfb1a1ec9c64.jpg)
2,这四个的不同之处有三点:
提示的图标、 显示的按钮、 返回值
openInfomation 类似i的图标 一个确定 void
openConfirm 问号图标 确定和取消 boolean
openQuestion 问号图标 是和否 boolean
openError 一个X的图标 确定 void
最后说一下,MessageDialog的大小不能根据内容自动调整,对于少量提示信息的时候显得大了些。
3,InputDialog输入的dialog
使用方法:
![](http://hiphotos.baidu.com/frjay/pic/item/f703918fab2e26c4503d9264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/513d2697562c135354fb9664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/55fbb2fb50d4431c4e4aea64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/43166d225a880e9a4723e864.jpg)
注意最后一个参数是"验证器",如果想验证数据可以提供一个验证器,并在这里使用。验证其的写法:
![](http://hiphotos.baidu.com/frjay/pic/item/4f4a20a451e127c99152ee64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/9052982269cc22cdd6cae264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/d7ca7bcb1584dc21bf09e664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/0a46f21f9fcb455df724e464.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/f6246b6029f1a97aeaf8f864.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/0c338744cc3a3472500ffe64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/510fd9f9fce84e67242df264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/d72a605908effa142934f064.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/252dd42a07f61aa5023bf664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/033b5bb5fa283bf037d3ca64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/c9ea15ce0311fb07b700c864.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/b6003af30345c76e342acc64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/3a87e9500ee8e8621138c264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/352ac65c2dfa7d7dfaf2c064.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/10385343c4309c2f9213c664.jpg)
然后改写定义语句为
![](http://hiphotos.baidu.com/frjay/pic/item/fbf2b211d2d19e40cb80c464.jpg)
二、上边说了MessageDialog不能自动调整大小,那什么能自动调整大小呢?答案是MessageBox.
同样继承自Dialog.
1,使用方法:MessageBox的使用方法稍微复杂,需要3句
![](http://hiphotos.baidu.com/frjay/pic/item/9313b07e89424b060dd7da64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/ca8065384915bf1d96ddd864.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/0cd79123d01f8f7aac34de64.jpg)
2,MessageBox同样可以控制图标和按钮数
是通过new的时候加入式样来实现的,例如
![](http://hiphotos.baidu.com/frjay/pic/item/97dda144e4f677bcb3b7dc64.jpg)
做过SWT开发的人对这种格式应该不陌生,到处都是。
图标的种类:
SWT.ICON_ERROR
SWT.ICON_INFORMATION
SWT.ICON_QUESTION
SWT.ICON_WARNING
按钮的组合:
SWT.OK
SWT.OK|SWT.CANCEL
SWT.YES|SWT.NO
SWT.YES|SWT.NO|SWT.CANCEL
SWT.RETRY|SWT.CANCEL
SWT.ABORT|SWT.RETRY|SWT.IGNORE
仔细看一下就是我们平时经常看到的各种对话框。
三、颜色选择对话框
不多说,使用方法几乎在swt中遍地都是的用法。
![](http://hiphotos.baidu.com/frjay/pic/item/ad345982f975fe9c0df4d264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/b2b7d0a241361f93caefd064.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/0cf431ad842d58374a36d664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/4b36acaf7d1f8df27dd92a64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/2edda3cc291ba32e00e92864.jpg)
注意color使用过后要dispose();
四、字体选择对话框
有了上边的一些经验其实在有eclipse这种编辑器的帮助下猜着就可以写了
![](http://hiphotos.baidu.com/frjay/pic/item/7cd98d10562b173f203f2e64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/01e9390178fd96307aec2c64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/213fb80e202e7ad937d12264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/36d12f2ee6fab56e4ec22664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/b9389b502e00fb5c84352464.jpg)
同样在结束后font.dispose();
五、打印对话框,同上的使用方法,同上的需要dispose
![](http://hiphotos.baidu.com/frjay/pic/item/4fc2d562e6f7cbe3e6113a64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/8535e5dd82d3bed177c63864.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/e71190ef110489d1cf1b3e64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/ce1b9d169638816ff2de3264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/fdfaaf519e1c9ab18d543064.jpg)
六、文件选择对话框:同上
1,实现方法
![](http://hiphotos.baidu.com/frjay/pic/item/f3deb48fe396bad0f11f3664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/8c5494ee81dd14172cf53464.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/f01f3a295e37cec098250a64.jpg)
2,如果想选择多个文件,加上SWT.MULTI
![](http://hiphotos.baidu.com/frjay/pic/item/9925bc312ba21a965fdf0e64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/5c6034a8271da38fca130c64.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/5edf8db1b0d1676a08230264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/cb13495474e1f36a574e0064.jpg)
3,加入filter
![](http://hiphotos.baidu.com/frjay/pic/item/0923dd54298cbc66d0090664.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/564e925850cb9de09d820464.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/9c82d158497d42bf810a1864.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/800a19d835fc9f0b32fa1c64.jpg)
4,保存对话框
保存对话框并不提供保存功能,这个需要自己实现,它只提供界面,并得到用户选择的目录和输入的文件名
![](http://hiphotos.baidu.com/frjay/pic/item/bc3eb135b838acb5a71e1264.jpg)
![](http://hiphotos.baidu.com/frjay/pic/item/33fa828b2bdc86edfc1f1064.jpg)
转载自:http://apps.hi.baidu.com/share/detail/15809844