TextField的范例4 : 文字 « SWT-JFace-Eclipse « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » SWT-JFace-Eclipse » 文字屏幕截图 
TextField的范例4

/**
  The following code is from Michael Schmidt(MichaelMSchmidt (at) msn.com).
  
  The code is published under BSD license.
  
  Thanks for the input from Michael Schmidt.
*/



package TextTest;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This is a revision of TextFieldExample4.  Two modifications have been made.
 * First, a Text value (ABC) has been added to the text2 field, simply for 
 * demonstration purposes.  Second, a mouseDown() listener has been added to 
 * text2.  Note that in the original sample, text2 would not highlight (perform
 * the selectAll() operation) when it gained focus with the mouse.  This is 
 * because clicking on the field invokes gainFocus() followed by mouseDown(), 
 * and mouseDown() does a clearSelection(), negating the selectAll() done in 
 * the gainFocus() method.  You can test this by simply commenting out the 
 * addMouseListener code.
 * <p>
 * Other modifications are possible.  The Text field may be designated 
 * read-only to prevent editing and erasing of the field when the tab key is 
 * used to move through the widgets.  Logic can also be used to do a selectAll()
 * when you first click on the field and then place the I-beam cursor if the 
 * mouse is clicked again. 
 
 @author Michael Schmidt
 *
 */
public class TextFieldExample4Revised {

  Display d;

  Shell s;

  TextFieldExample4Revised() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250250);
    s.setText("A Text Field Example");
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(1005010020);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(1007510020);
    text2.setTextLimit(30);
    text2.setText("ABC");

    // add a focus listener
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Texte.widget;
        t.selectAll();
      }

      public void focusLost(FocusEvent e) {
        Text t = (Texte.widget;
        if (t.getSelectionCount() 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    
    //  Added code - comment out to test without the mouseListener
   text2.addMouseListener(new MouseAdapter() {
    public void mouseDown(final MouseEvent e) {
      Text t = (Texte.widget;
      t.selectAll();
    }
  });
    // End of added code
   
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }

  public static void main(String[] arg) {
    new TextFieldExample4Revised();
  }

}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class TextFieldExample4 {

  Display d;

  Shell s;

  TextFieldExample4() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250250);
    s.setText("A Text Field Example");
    Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);
    text1.setBounds(1005010020);
    text1.setTextLimit(5);
    text1.setText("12345");
    Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);
    text2.setBounds(1007510020);
    text2.setTextLimit(30);

    // add a focus listener
    FocusListener focusListener = new FocusListener() {
      public void focusGained(FocusEvent e) {
        Text t = (Texte.widget;
        t.selectAll();
      }

      public void focusLost(FocusEvent e) {
        Text t = (Texte.widget;
        if (t.getSelectionCount() 0) {
          t.clearSelection();
        }
      }
    };
    text1.addFocusListener(focusListener);
    text2.addFocusListener(focusListener);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }

  public static void main(String[] arg) {
    new TextFieldExample4();
  }

}


           
       
Related examples in the same category
1. 文字大写
2. 文本事件文本事件
3. 示范文本和标签示范文本和标签
4. 折线折线
5. 备注文本备注文本
6. 显示文本字段显示文本字段
7. 多行注释
8. 显示红色字符,使用LineStyleListener显示红色字符,使用LineStyleListener
9. TextField的范例5TextField的范例5
10. TextField的范例3TextField的范例3
11. TextField的范例2TextField的范例2
12. TextField的范例TextField的范例
13. SWT XML编辑:修改DOMSWT XML编辑:修改DOM
14. 在Shell窗口绘制国际化的文字风格在Shell窗口绘制国际化的文字风格
15. 发现当用户滚动文字事件发现当用户滚动文字事件
16. 验证输入(日期格式)验证输入(日期格式)
17. 验证输入(只允许数字)验证输入(只允许数字)
18. 选择(启动,停止)选择(启动,停止)
19. 文字控件范例:设置选择(工字钢)文字控件范例:设置选择(工字钢)
20. 选择所有文本控件中的文字选择所有文本控件中的文字
21. 调整文字控制(显示约10个字符)调整文字控制(显示约10个字符)
22. 提示密码(设置回声字符)提示密码(设置回声字符)
23. 停止回车的默认按钮停止回车的默认按钮
24. 添加一个菜单项,选择所有的控制添加一个菜单项,选择所有的控制
25. 在文本框控件或组合控件检测CR(默认选择)在文本框控件或组合控件检测CR(默认选择)
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.