Plug-in 对话框

来源:互联网 发布:easymule mac 编辑:程序博客网 时间:2024/06/03 19:24

Plug-in 对话框

首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。

1、标准消息对话框

MessageBox可以方便地生成标准消息对话框。
setText方法设置MessageBox的标题,setMessage方法设置显示消息。
MessageBox的图标样式,
  • ICON_ERROR,显示错误消息。
  • ICON_WARNING,显示警告消息。
  • ICON_INFORMATION,显示提示信息。
  • ICON_WORKING,显示正在进行信息。
  • ICON_QUESTION,显示问题信息。
MessageBox的按钮样式,默认是SWT.OK。也可以是下列组合,只能选择一种组合或同一组合中几个按钮,否则只会显示确定按钮。
  • SWT.OK | SWT.CANCEL,确定 | 取消
  • SWT.YES | SWT.NO | SWT.CANCEL,是 | 否 | 取消
  • SWT.ABORT | SWT.RETRY | SWT.IGNORE,中止 | 重试 | 忽略
获取open方法的返回值,判断用户单击的按钮。
Button defaultButton = new Button(shell, SWT.NONE);defaultButton.setText("MessageBox");defaultButton.setBounds(10, 10, 150, 25);defaultButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageBox mb = new MessageBox(shell, SWT.ICON_SEARCH);mb.setText("MessageBox");mb.setMessage("MessageBox open, MessageBox open!");mb.open();}});Button okOrCancelButton = new Button(shell, SWT.NONE);okOrCancelButton.setText("Ok|Cancel");okOrCancelButton.setBounds(10, 45, 150, 25);okOrCancelButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageBox mb = new MessageBox(shell,SWT.ICON_ERROR|SWT.OK|SWT.CANCEL);mb.setText("Error");mb.setMessage("A error occur!");mb.open();}});Button yesOrNoButton = new Button(shell, SWT.NONE);yesOrNoButton.setText("yes|no");yesOrNoButton.setBounds(10, 80, 150, 25);yesOrNoButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageBox mb = new MessageBox(shell,SWT.ICON_INFORMATION|SWT.YES|SWT.NO|SWT.CANCEL);mb.setText("Information");mb.setMessage("You got a message!");mb.open();}});Button retryButton = new Button(shell, SWT.NONE);retryButton.setText("Retry");retryButton.setBounds(10, 115, 150, 25);retryButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageBox mb = new MessageBox(shell,SWT.ICON_QUESTION|SWT.ABORT|SWT.RETRY|SWT.IGNORE);mb.setText("Question");mb.setMessage("Do you want to try again?");int result = mb.open();if (result == SWT.ABORT) {System.out.println("ABORT Pressed");} else if (result == SWT.RETRY) {System.out.println("RETRY Pressed");} else if (result == SWT.IGNORE) {System.out.println("IGNORE Pressed");} else {System.out.println(result + " Pressed");}}});
显示如下

      

  

2、文本和目录对话框

FileDialog是文本对话框,有三种样式可以选择,SWT.OPEN|SWT.MULTI|SWT.SAVE。样式是OPEN和MULTI时,显示为打开。样式是SAVE时,显示为另存为。
setFilterNames和setFilterExtensions为文件对话框设置过滤器,setFilterPath方法设置默认打开目录。
DirectoryDialog是目录对话框,setFilterPath方法设置默认打开目录。

Button openButton = new Button(shell, SWT.NONE);openButton.setText("Open");openButton.setBounds(10, 10, 150, 25);openButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {FileDialog dialog = new FileDialog(shell);dialog.setFilterPath("C:\\");dialog.setFilterNames(new String[]{"Java Files", "Class Files", "All Files"});dialog.setFilterExtensions(new String[]{"*.java", "*.class", "*.*"});String result = dialog.open();System.out.println(result);}});Button multiButton = new Button(shell, SWT.NONE);multiButton.setText("Multi");multiButton.setBounds(10, 45, 150, 25);multiButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {FileDialog dialog = new FileDialog(shell, SWT.MULTI);String result = dialog.open();if (result != null) {for (String fileName : dialog.getFileNames()) {System.out.println(fileName);}}}});Button saveButton = new Button(shell, SWT.NONE);saveButton.setText("Save");saveButton.setBounds(10, 80, 150, 25);saveButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {FileDialog dialog = new FileDialog(shell, SWT.SAVE);dialog.setFilterPath("d:\\");String result = dialog.open();System.out.println(result);}});Button dirButton = new Button(shell, SWT.NONE);dirButton.setText("Directory");dirButton.setBounds(10, 115, 150, 25);dirButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {DirectoryDialog dialog = new DirectoryDialog(shell);dialog.setFilterPath("d:\\");String result = dialog.open();System.out.println(result);}});

显示如下



3、颜色对话框

颜色对话框显示一个调色板并允许用户选择其中一种颜色。
final Button colorButton = new Button(shell, SWT.NONE);colorButton.setText("Color Dialog");colorButton.setBounds(10, 10, 150, 25);colorButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {ColorDialog dialog = new ColorDialog(shell);RGB rgb = dialog.open();if (rgb != null) {System.out.println(rgb.red + ":" + rgb.green + ":" + rgb.blue);Color newColor = new Color(display, rgb);shell.setBackground(newColor);if (color != null) color.dispose();color = newColor;}}});... ...if (color != null) {color.dispose();}

显示如下


4、字体对话框

字体对话框显示当前系统中所有可用字体供用户选择。
final Button fontButton = new Button(shell, SWT.NONE);fontButton.setText("Font Dialog");fontButton.setBounds(10, 10, 150, 25);fontButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {FontDialog dialog = new FontDialog(shell);FontData fd = dialog.open();if (fd != null) {Font newFont = new Font(display, fd);fontButton.setFont(newFont);if (font != null) font.dispose();font = newFont;}}});... ...if (font != null) {font.dispose();}

显示如下


5、MessageDialog

MessageDialog对话框有多种形式。其中confirm和question需要通过返回值来判断结果。
Button confirmButton = new Button(shell, SWT.NONE);confirmButton.setText("Confirm Dialog");confirmButton.setBounds(10, 10, 150, 25);confirmButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {boolean result = MessageDialog.openConfirm(shell,"MessageDialog", "Confirm Dialog");System.out.println(result);}});Button errorButton = new Button(shell, SWT.NONE);errorButton.setText("Error Dialog");errorButton.setBounds(10, 45, 150, 25);errorButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageDialog.openError(shell, "MessageDialog", "Error Dialog");}});Button infoButton = new Button(shell, SWT.NONE);infoButton.setText("Info Dialog");infoButton.setBounds(10, 80, 150, 25);infoButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageDialog.openInformation(shell, "MessageDialog", "Info Dialog");}});Button questionButton = new Button(shell, SWT.NONE);questionButton.setText("Qurstion Dialog");questionButton.setBounds(10, 115, 150, 25);questionButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {boolean result = MessageDialog.openQuestion(shell,"MessageDialog", "Qurstion Dialog");System.out.println(result);}});Button warningButton = new Button(shell, SWT.NONE);warningButton.setText("Warning Dialog");warningButton.setBounds(10, 150, 150, 25);warningButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MessageDialog.openWarning(shell, "MessageDialog", "Warning Dialog");}});

显示如下

6、InputDialog对话框接受输入

Button inputButton = new Button(shell, SWT.NONE);inputButton.setText("Input Dialog");inputButton.setBounds(10, 10, 150, 25);inputButton.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {InputDialog dialog = new InputDialog(shell, "输入", "3 + 5 = ?", "", null);int result = dialog.open();if (result == InputDialog.OK) {System.out.println(dialog.getValue());}}});

显示如下


0 0
原创粉丝点击