`

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

    博客分类:
  • Java
 
阅读更多

焦点事件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();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics