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();
}
}