锁定老帖子 主题:Swt/Jface中提供的dialog
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-02-23
Swt/Jface中提供了各种各样的Dialog,一般情况下就能满足我们的需求,现在把这些dialog介绍一下, MessageDialog.openInfomation(shell,title,message); MessageDialog.openConfirm(shell,title,message); MessageDialog.openQuestion(shell,title,message); MessageDialog.openError(shell,title,message); 2,这四个的不同之处有三点: 最后说一下,MessageDialog的大小不能根据内容自动调整,对于少量提示信息的时候显得大了些。
InputDialog inputDialog = new InputDialog(shell,"标题","提示信息","默认值",null);
if(inputDialog.open() == InputDialog.OK){ String value = dialog.getValue(); } 注意最后一个参数是"验证器",如果想验证数据可以提供一个验证器,并在这里使用。验证其的写法:
public class MyValidator implements IInputValidaor{
public String isValid(String newText){ float value = 0; try{ value = Float.valueOf(newText).floatValue(); }catch(NumberFormatException e){ return "错误:请输入数值"; } if(value>0 && value<100){ return null; }else{ return "错误:请输入大于0,小于100的数"; } } }
InputDialog inputDialog = new InputDialog(shell,"标题","提示信息","默认值",new MyValidator());
二、上边说了MessageDialog不能自动调整大小,那什么能自动调整大小呢?答案是MessageBox.
MessageBox mb = new MessageBox(shell);
mb.setMessage("提示信息"); mb.open(); 2,MessageBox同样可以控制图标和按钮数
MessageBox mb = new MessageBox(shell,SWT._ICON_QUEST|SWT.YES|SWT.NO);
做过SWT开发的人对这种格式应该不陌生,到处都是。 按钮的组合:
ColorDialog dialog = new ColorDialog(shell);
RGB rgb = dialog.open(); if(rgb != null){ Color color = new Color(shell.getDisplay(),rgb); }
FontDialog dialog = new FontDialog(shell);
FontData fontData = dialog.open(); if (fontDate != null){ Font font = new Font(shell.getDisplay(),fontData); }
PrintDialog dialog = new PrintDialog(shell);
PrintData printData = dialog.open(); if (printDate != null){ Printer printer = new Printer(printerData); }
FileDialog dialog = new FileDialog(shell,SWT.OPEN);
dialog.setFilterPath("c:\windows");//设置初始路径 String fileName = dialog.open();//返回的全路径(路径+文件名) 2,如果想选择多个文件,加上SWT.MULTI
FileDialog dialog = new FileDialog(shell,SWT.OPEN|SWT.MULTI);
String fileName = dialog.open();//返回最后一个选择文件的全路径 String[] fileNames = dialog.getFileNames();//返回所有选择的文件名,不包括路径 String path = dialog.getFilterPath();返回选择的路径,这个和fileNames配合可以得到所有的文件的全路径 3,加入filter
FileDialog dialog = new FileDialog(shell,SWT.OPEN|SWT.NULTI);
dialog.setFilterNames(new String[]{"可执行文件(*.exe),"Excel(*.xls),"所有文件""}); dialog.setFilterExtensions(new String[]{"*.exe","*.xls","*.*"}); //接受方法同2中的写法,不同就是加入了过滤器。 4,保存对话框
FileDialog dialog = new FileDialog(shell,SWT.SAVE);
String fileName = dialog.open(); 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 3254 次