首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 开源 FAQ 第二书店 博文视点 程序员
频道: 研发 数据库 中间件 信息化 视频 .NET Java 游戏 移动 服务: 人才 外包 培训
    图书品种:235680
       
热门搜索: ASP.NET Ajax Spring Hibernate Java

15.5  输入对话框 

如果要实现用户和应用程序的交互,通常用户输入一些相关的信息,应用程序会根据这些信息进行操作,InputDialog实现了输入对话框的功能。

15.5.1  输入对话框的功能

新建输入对话框(InputDialog)要创建一个InputDialog实例。InputDialog构造函数为:“public InputDialog(Shell parentShell, String dialogTitle,String dialogMessage, String initialValue, IInputValidator validator)”,解释如下。

l   parentShell:表示输入框的父窗口。

l   dialogTitle:表示输入框的标题。

l   dialogMessage:表示输入框的提示信息。

l   initialValue:表示初始输入框的值。

l   validator:表示输入框的校验器。

校验器是一个IInputValidator类型的对象(也可以为空),校验器中要实现isValid方法,该方法返回一个String类型的值或null,返回值为String类型时为错误的提示信息,如果为null则表示校验正确。下面代码为isValid的实现。

  public String isValid(String newText) {

    int len = newText.length();

    if (len < 5) return "Too short";

    if (len > 8) return "Too long";

    return null;

  }

上面代码表示如果newText的长度大于8或小于5时校验出错,提示错误信息为“Too short”或“Too long”,窗口的确定按钮为禁用。

15.5.2  输入对话框实例

为了更好地理解输入对话框,下面通过实例来介绍。实例中新建一个输入对话框,并加入了校验器,代码如例程15-7所示。

例程15-7  InputDialogExample.java

public class InputDialogExample extends ApplicationWindow {

    

     public InputDialogExample() {

         super(null);

     }

     public void run() {

         setBlockOnOpen(true);

         open();

         Display.getCurrent().dispose();

     }

     protected void configureShell(Shell shell) {

         super.configureShell(shell);

         shell.setText("Get Input");

     }

     protected Control createContents(Composite parent) {

         Composite composite = new Composite(parent, SWT.NONE);

         composite.setLayout(new GridLayout(1, false));

         final Label label = new Label(composite, SWT.NONE);

         label.setText("This will display the user input from InputDialog");

         Button show = new Button(composite, SWT.PUSH);

         show.setText("Get Input");

         show.addSelectionListener(new SelectionAdapter() {

              public void widgetSelected(SelectionEvent event) {

                   //新建输入对话框,并添加校验器

                   InputDialog dlg = new InputDialog(Display.getCurrent()

                            .getActiveShell(), "", "Enter 5-8 characters", label

                            .getText(), new LengthValidator());

                   //打开输入对话框

                   if (dlg.open() == Window.OK) {

                       // 如果用户单击“OK”按钮,则给标签赋值

                       label.setText(dlg.getValue());

                   }

              }

         });

         parent.pack();

         return composite;

     }

     public static void main(String[] args) {

         new InputDialogExample().run();

     }

}

class LengthValidator implements IInputValidator {

     //校验用户的输入,newText为对话框的输入值

     public String isValid(String newText) {

         int len = newText.length();

         if (len < 5)

              return "Too short";

         if (len > 8)

              return "Too long";

         //输入数据正确

         return null;

     }

}

上例为输入对话框添加了一个校验器,程序运行效果如图15-5所示。图中输入数据“very good”太长,校验未通过,确定按钮(OK)为禁用。

图15-5  输入对话框

查看所有评论(0)条】

最近评论



正在载入评论列表...
热点评论