SWT對話框
對話框,都繼承自org.eclipse.swt.widgets.Dialog,有Modal的和Modeless的區(qū)分,一般的對話框處理程序如下:
<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(),返回的是點擊的按鈕對應(yīng)的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()