第6节:SWT事件处理

#SWT事件处理

##SWT事件处理的方法 ###方法1:匿名内部类方式添加按钮被点击事件

		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageDialog.openInformation(shell, "hello", "匿名类方式");
			}
		});

###方法2:定义命名内部类 定义内部类

	private static final class ButtonSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
			MessageDialog.openInformation(shell, "hello", "命名内部类方式");
		}
	}

调用:

button.addSelectionListener(new ButtonSelectionListener());

###方法3:还可以定义外部类,直接将命名内部类提取出重新生成一个java文件 定义外部类:

package cn.haibin.rcp.test.oscblog;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Shell;

/**
 * 定义按钮选择事件类
 * 
 * @author cn.haibin
 *
 */
public class ButtonSelectionListener extends SelectionAdapter {

	Shell shell = new Shell();

	@Override
	public void widgetSelected(SelectionEvent e) {
		MessageDialog.openInformation(shell, "hello", "命名内部类方式");
	}
}

调用

static ButtonSelectionListener buttonSelectionListener = new ButtonSelectionListener();

button.addSelectionListener(buttonSelectionListener);

###方法4:实现监听接口,即让HelloWorld本身继承SelectionAdapter或继承自ButtonSelectionLister接口,再重新或实现相应方法

package cn.haibin.rcp.test.oscblog;

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

/**
 * 功能说明:继承SelectionAdapter实现按钮单击事件
 * 
 * @author cn.haibin
 *
 */
public class SwtBtnHelloWorld extends SelectionAdapter {

	private static final Shell shell = new Shell(); // shell是程序的主窗口

	@Override
	public void widgetSelected(SelectionEvent e) {
		MessageDialog.openInformation(shell, "hello", "继承SelectionAdapter");
	}

	public static void main(String[] args) {
		final Display display = Display.getDefault();// Display多线程操作负责管理事件循环和控制UI线程和其它线程之间的通信
		shell.setSize(300, 200); // 设置主窗口的大小
		shell.setText("HelloWorld"); // 设置主窗口的标题

		// 创建主窗口的其它界面组件
		Button button = new Button(shell, SWT.NONE); // 创建一个按钮对象
		button.setText("HelloWorld"); // 设置按钮文字
		button.setBounds(100, 50, 100, 50); // 设置按钮在窗体中位置和大小

		shell.layout(); // 应用界面布局
		shell.open(); // 打开shell主窗口

		while (!shell.isDisposed()) {// 如果shell主窗口没有关闭,则一直循环
			if (!display.readAndDispatch())
				display.sleep(); // 如果display不忙,就让display处于休眠状态
		}

		display.dispose(); // 释放display资源
	}
}

##SWT事件处理方法的比较

  • 匿名类 优点:内部类更方便些, 缺点: 1、不适合于写太多的代码,一般不超过十行 2、事件随着组件一起,代码块比较分散,不便于阅读和维护, 3、视觉上显得散乱, 4、无法复用事件处理代码
  • 命名内部类 从代码书写,阅读,维护以及程序的可扩展性来看比较适合推荐
  • 外部类 一般是考虑代码重用才使用;
  • 实现监听接口的方式 一般不建议使用 ##如何在事件方法中访问变量
  • 变量加final修饰符
  • 将变量写成类的实例变量或类变量
  • 将事件写成命名内部类,然后通过构造方法的参数传入 ##最常用的事件监听器
  • addSelectionListener widgetSelected:当组件被选择(单击鼠标、按回车键)时触发此方法的事件处理程序。 widgetDefaultSelected:用于某些很少触发选择事件的组件,在实际开发中很少用。例如:文本框回车事件、列表框双击事件等,只能用:widgetDefaultSelected,用widgetSelected方法无效。
  • addKeyListener keyPressed:当前焦点停在组件时,按下键盘任一键时触发,但对某些控件,如Button,按回车键无法执行此方法 keyReleased:按键弹起时触发。
  • addFocusListener focusGained:得到焦点触发 focusLost:失去焦点出发
  • addMouseListener mouseDown:鼠标按下时触发; mouseUp:鼠标放下时触发 mouseDoubleClick:鼠标双击时触发
  • DisposeListener
    widgetDisposed:当组件被销毁时触发该方法
  • Listener 利用通用监听器可以监听一些组件没有明确提供的事件,一个Listener也可以监听多个事件

转载于:https://my.oschina.net/u/2494556/blog/689028

你可能感兴趣的:(第6节:SWT事件处理)