验证输入(格式的日期) : 文本事件 « SWT « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » SWT » 文本事件 
17. 18. 7. 验证输入(格式的日期)
验证输入(格式的日期)
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Text example snippet: verify input (format for date)
 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TextVerifyInputFormatDate {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.BORDER);
    text.setText("YYYY/MM/DD");
    ;
    final Calendar calendar = Calendar.getInstance();
    text.addListener(SWT.Verify, new Listener() {
      boolean ignore;

      public void handleEvent(Event e) {
        if (ignore)
          return;
        e.doit = false;
        StringBuffer buffer = new StringBuffer(e.text);
        char[] chars = new char[buffer.length()];
        buffer.getChars(0, chars.length, chars, 0);
        if (e.character == '\b') {
          for (int i = e.start; i < e.end; i++) {
            switch (i) {
            case 0/* [Y]YYY */
            case 1/* Y[Y]YY */
            case 2/* YY[Y]Y */
            case 3/* YYY[Y] */{
              buffer.append('Y');
              break;
            }
            case 5/* [M]M */
            case 6/* M[M] */{
              buffer.append('M');
              break;
            }
            case 8/* [D]D */
            case 9/* D[D] */{
              buffer.append('D');
              break;
            }
            case 4/* YYYY[/]MM */
            case 7/* MM[/]DD */{
              buffer.append('/');
              break;
            }
            default:
              return;
            }
          }
          text.setSelection(e.start, e.start + buffer.length());
          ignore = true;
          text.insert(buffer.toString());
          ignore = false;
          text.setSelection(e.start, e.start);
          return;
        }

        int start = e.start;
        if (start > 9)
          return;
        int index = 0;
        for (int i = 0; i < chars.length; i++) {
          if (start + index == || start + index == 7) {
            if (chars[i== '/') {
              index++;
              continue;
            }
            buffer.insert(index++, '/');
          }
          if (chars[i'0' || '9' < chars[i])
            return;
          if (start + index == && '1' < chars[i])
            return/* [M]M */
          if (start + index == && '3' < chars[i])
            return/* [D]D */
          index++;
        }
        String newText = buffer.toString();
        int length = newText.length();
        StringBuffer date = new StringBuffer(text.getText());
        date.replace(e.start, e.start + length, newText);
        calendar.set(Calendar.YEAR, 1901);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        String yyyy = date.substring(04);
        if (yyyy.indexOf('Y'== -1) {
          int year = Integer.parseInt(yyyy);
          calendar.set(Calendar.YEAR, year);
        }
        String mm = date.substring(57);
        if (mm.indexOf('M'== -1) {
          int month = Integer.parseInt(mm1;
          int maxMonth = calendar.getActualMaximum(Calendar.MONTH);
          if (> month || month > maxMonth)
            return;
          calendar.set(Calendar.MONTH, month);
        }
        String dd = date.substring(810);
        if (dd.indexOf('D'== -1) {
          int day = Integer.parseInt(dd);
          int maxDay = calendar.getActualMaximum(Calendar.DATE);
          if (> day || day > maxDay)
            return;
          calendar.set(Calendar.DATE, day);
        else {
          if (calendar.get(Calendar.MONTH== Calendar.FEBRUARY) {
            char firstChar = date.charAt(8);
            if (firstChar != 'D' && '2' < firstChar)
              return;
          }
        }
        text.setSelection(e.start, e.start + length);
        ignore = true;
        text.insert(newText);
        ignore = false;
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}
17. 18. 文本事件
17. 18. 1. 添加焦点/输出活动,文字添加焦点/输出活动,文字
17. 18. 2. 新增KeyEvent事件监听新增KeyEvent事件监听
17. 18. 3. 监听键盘事件监听键盘事件
17. 18. 4. Use a regular expression to verify input(a phone number is used)Use a regular expression to verify input(a phone number is used)
17. 18. 5. Add events to Text: MouseDown, MouseMove, MouseUp, KeyDown, KeyUp ResizeAdd events to Text: MouseDown, MouseMove, MouseUp, KeyDown, KeyUp Resize
17. 18. 6. 监听用户滚动文字控件监听用户滚动文字控件
17. 18. 7. 验证输入(格式的日期)验证输入(格式的日期)
17. 18. 8. 验证输入(只允许数字)验证输入(只允许数字)
17. 18. 9. Text TraverseListener: Tab key event (override Tab behavior to traverse out of a Text)Text TraverseListener: Tab key event (override Tab behavior to traverse out of a Text)
17. 18. 10. 文字默认选择事件(返回/回车键)文字默认选择事件(返回/回车键)
17. 18. 11. 添加默认选择事件监听文字控件添加默认选择事件监听文字控件
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.