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

1.鼠标MouseListener有三个方法:

mouseDown:摁下触发;

mouseUp:松开时;

mouseDoubleClick:双击时;

左中右键都可

2.属性:

button:int:左=1;中=2;右=3;

stateMask:同KeyListener;

x,y:摁下处坐标

package com.iteye.niewj.swt.chapter1;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MouseEventShow {

	/**
	 * @author niewj
	 * @since 2012-6-4 
	 */
	public static void main(String[] args) {
//		外shell
		Display display = Display.getDefault();
		final Shell shell = new Shell(display);
		shell.setBounds(20, 100, 450, 500);
		shell.setText("鼠标事件");
		
//		布局管理器
		RowLayout layout = new RowLayout();
		layout.spacing=30;
		layout.marginWidth=30;
		layout.marginHeight=40;
		layout.wrap=true;
		layout.type=SWT.VERTICAL;
		shell.setLayout(layout);
		
//		RowData控制组件大小和隐藏
		//*text
		RowData textData = new RowData();
//		true隐藏组件,所占空间被忽略,下(后)面上(前)移
		textData.exclude=false;
		textData.height=150;
		textData.width=300;
		
		//*button
		RowData btnData = new RowData();
		btnData.exclude=false;
		btnData.height=30;
		btnData.width=200;

		//1.上button
		final Button button = new Button(shell, SWT.NONE);
		button.setText("上面按钮");
		button.setLayoutData(btnData);
		//2.中文本
		final Text text = new Text(shell, SWT.NONE);
		text.setText("文本框框");
		text.setLayoutData(textData);
		//3.下button
		final Button button2 = new Button(shell, SWT.NONE);
		button2.setText("下面按钮");
		button2.setLayoutData(btnData);
		
		text.addMouseListener(new MouseListener() {
			@Override
			public void mouseUp(MouseEvent e) {
				String msg = "";
				switch (e.button) {
				case 1:
					msg = "左键";
					break;
				case 2:
					msg = "中键";
					break;
				case 3:
					msg = "右键";
					break;
				}
				text.setText("松开文本框;事件源为:"+msg);
			}
			@Override
			public void mouseDown(MouseEvent e) {
				String msg = "";
				switch (e.button) {
				case 1:
					msg = "左键";
					break;
				case 2:
					msg = "中键";
					break;
				case 3:
					msg = "右键";
					break;
				}
				text.setText("摁下文本框,源为:"+msg);
			}
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				String msg = "";
				switch (e.button) {
				case 1:
					msg = "左键";
					break;
				case 2:
					msg = "中键";
					break;
				case 3:
					msg = "右键";
					break;
				}
				text.setText("双击文本框,事件源为:"+msg);
			}
		});
		
//		显示
		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、付费专栏及课程。

余额充值