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

SelectionListener是最常用的事件监听器。

有两个方法:widgetSelected和widgetDefaultSelectd

widgetSelected监听的事件包括:button被单击或组件被选中时。

widgetDefaultSelected监听的事件包括【较少用到的】:文本框回车事件,列表框回车事件

 

一个按钮单击的例子:

package com.iteye.niewj.swt.chapter1;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HelloSWT {

	/**
	 * @author niewj
	 * @since 2012-6-1 
	 */
	public static void main(String[] args) {
		Display display = new Display();
		//1.一个窗口
		final Shell shell = new Shell(display);
		shell.setSize(250, 300);
		shell.setText("Hello SWT");
		//2.一个按钮
		Button button = new Button(shell, SWT.NONE);
		button.setText("!点^.^我!");
		button.setLocation(70, 150);
		button.setSize(100,25);

		//3.按钮的选中事件:按钮添加事件
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				MessageDialog.openInformation(shell, "你好", "二B青年");
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {}
		});
		
		//4.设置显示
		shell.layout();
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		display.dispose();
	}
}


 

posted @ 2012-06-01 17:21  niewj  阅读(1271)  评论(0编辑  收藏  举报