SWT常用的事件监听器之【FocusListener】

焦点事件FocusListener有如下两个方法:

focusGained() 得到焦点触发

focusLost() 失去焦点触发

举例:

package com.iteye.niewj.swt.chapter1;

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

public class FocusEventShow {

	/**
	 * @author niewj
	 * @since 2012-6-4 
	 */
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell();
		shell.setText("焦点事件");
		shell.setBounds(30,100,300,400);
		
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("文本框1");
		label.setBounds(10,50,80,25);
		
		final Text text1 = new Text(shell, SWT.NONE);
		text1.setBounds(100,50,200,25);
		
		Label label2 = new Label(shell, SWT.NONE);
		label2.setText("文本框2");
		label2.setBounds(10,80,80,25);
		
		final Text text2 = new Text(shell, SWT.NONE);
		text2.setBounds(100,80,200,25);
		
		/* text1得失去焦点时触发事件 */
		text1.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent arg0) {
				text1.setText("文本框1--失焦");
			}
			
			@Override
			public void focusGained(FocusEvent arg0) {
				text1.setText("文本框1--得焦");
			}
		});

		/* text2得失去焦点时触发事件 */
		text2.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent arg0) {
				text2.setText("文本框2--失焦");
			}
			
			@Override
			public void focusGained(FocusEvent arg0) {
				text2.setText("文本框2--得焦");
			}
		});
		
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值