即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Plug-in 对话框

编程语言 chennai1101 16℃ 0评论
本文目录
[隐藏]

1.Plug-in 对话框

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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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() {
 @Override
 public 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();
}

显示如下






转载请注明:CodingBlog » Plug-in 对话框

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情