文本修改事件——VerifyEvent和ModifyEvent

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;//VerifyEvent
import org.eclipse.swt.layout.*;
public class I implements VerifyListener{
	private Shell shell;
	private Text top;
	private Text bottom;
	public I(){
		shell=new Shell();
		shell.setText("文本改变事件实例");
		shell.setLayout(new GridLayout(2,false));
		new Label(shell,SWT.NONE).setText("请输入小写字母");
		top=new Text(shell,SWT.SINGLE);
		top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		top.addVerifyListener(this);
		
		new Label(shell,SWT.NONE).setText("请输入大写字母");
		bottom=new Text(shell,SWT.SINGLE);
		bottom.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		bottom.setEditable(false);
		shell.setSize(300,100);
	}
	public Shell getShell(){
		return shell;
	}
	public void setShell(Shell shell){
		this.shell=shell;
	}
	//VerifyListener接口中的方法
	public void verifyText(VerifyEvent e){
		char c=e.character;//获得输入的字符
		//如果该字符不为字母或者不是大写字母,取消输入
		if(!Character.isLetter(c)||Character.isUpperCase(c)){
			e.doit=false;
			return;
		}
		//在下边的文本框中输入相对应的大写字母
		bottom.append(""+Character.toUpperCase(c));
		
	}
	public static void main(String[] args){
		Display display=Display.getDefault();
		I sample=new I();
		sample.getShell().open();
		while(!sample.getShell().isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}
	
	
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值