SWT对话框

SWT对话框

对话框,都继承自org.eclipse.swt.widgets.Dialog,有Modal的和Modeless的区分,一般的对话框处理程序如下:

<DialogType>
dlg = new <DialogType>(shell);
dlg.setSomeData(data);

<ReturnType>
returnValue = dlg.open();
if (returnValue == null) {
// User clicked cancel
} else {
// Do something with returnValue
}

对话框主要有以下六种:

1、MessageBox,消息对话框

可定制的对话框样式包括:

标题栏:setText()

消息提示:setMessage()

消息图标类型:style属性,有:SWT.ICON_ERROR,SWT.ICON_INFORMATION,SWT.ICON_QUESTION,SWT.ICON_WARNING,SWT.ICON_WORKING

按钮类型:style属性,有: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

对话框返回值:int open(),返回的是点击的按钮对应的int值。

示例:

MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION |SWT.YES | SWT.NO);
messageBox.setMessage(”Is this question simple?”);
int rc = messageBox.open();

2、ColorDialog,选择颜色对话框

ColorDialog dlg = new ColorDialog(shell);
RGB rgb = dlg.open();
if (rgb != null) {
Color color = new Color(shell.getDisplay(), rgb);
}

3、DirectoryDialog,文件夹选择对话框

DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setFilterPath(text.getText());
dlg.setText(”SWT’s DirectoryDialog”);
dlg.setMessage(”Select a directory”);
String selectedDirectory = dlg.open();

4、FileDialog,文件选择对话框

FileDialog dlg = new FileDialog(shell, SWT.OPEN);
String fileName = dlg.open();
if (fileName != null) {
// Open the file
}

对话框标题栏:void setText(String text)

文件后缀名过滤:void setFilterExtensions (String[] extensions)

缺省路径及文件名:void setFilterPath(String string)

返回值:String[] getFileNames() / String getFileName()

5、FontDialog,字体选择对话框

FontDialog dlg = new FontDialog(shell);
FontData fontData = dlg.open();
if (fontData != null) {
Font font = new Font(shell.getDisplay(), fontData);
Color color = new Color(shell.getDisplay(), dlg.getRGB());
myLabel.setFont(font);
myLabel.setForeground(color);
}

6、PrintDialog,打印对话框

如何自定义对话框呢?扩展Dialog类即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值