本文共 4219 字,大约阅读时间需要 14 分钟。
什么是事件?点击鼠标是一个事件,按下一个按钮也一个事件,关闭一个窗口也是一个事件。
什么是监听器?监听器就是监听事件什么时候发生的,用来控制事件发生的具体动作。(个人见解) 事件产生处的SWT组件称为事件源,(官方) 对事件作出具体动作称为监听器(Listener)。监听器负责监听组件上的事件,并对发生的事件进行处理。基本的模式是将一个监听器添加到已经创建的组件中,当相应的事件发生时,监听器的代码就会被执行。SWT 的常用事件
每一种类型的监听器,都有一个接口来定义这种监听器,由类提供事件信息,由应用程序接口方法负责添加监听器。如果一个监听器接口中定义了多个方法,则会提供一个适配器来实现监听器接口并同时提供空方法。所有的事件、监听器和适配器都放在包org.eclipse.swt.events中。例如,添加组件选择事件的监听器为addSelectionListener,事件为
SelectionEvent,相应的适配器为SelectionAdapter。添加鼠标事件的监听器为addMouseListener,事件为MouseEvent,相应的适配器为MouseAdapter。SWT中常用的事件如下: 1.addMouseListener 鼠标监听器。常用方法: mouseDown() 鼠标按下时触发。 mouseUP() 鼠标放开时触发。 mouseDoubleClick() 鼠标双击时触发。 2.addKeyListener 按键监听器。常用方法: keyPressed() 当焦点在组件上时,按下键盘任一键时触发。但对某些组件(如按钮Button),按回车键时不能触发。keyReleased() 按键弹起时触发。 3.addSelectionListener 组件选择监听器。常用方法: widgetSelected() 当组件被选择(单击鼠标、焦点在组件上时按回车键)时触发。 4.addFocusListener 焦点监听器。常用方法: focusGained() 得到焦点时触发。 focusLost() 失去焦点时触发。SWT 的常用监听器应用实例
鼠标监听器,监听鼠标双击事件。package edu.ch4;import org.eclipse.swt.widgets.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.events.*;import org.eclipse.swt.SWT;public class Sample4_18 {static Text text;public static void main(String[] args) {Display display = new Display();final Shell shell = new Shell(display);RowLayout rowLayout=new RowLayout();rowLayout.marginWidth=20;rowLayout.marginHeight=30;shell.setLayout(rowLayout);shell.setText("SWT事件处理示例");PDF 文件使用 "pdfFactory" 试用版本创建www.fineprint.cntext=new Text(shell,SWT.BORDER|SWT.WRAP);RowData rowData=new RowData();rowData.width=100;rowData.height=50;text.setLayoutData(rowData);//将鼠标监听器用于text组件text.addMouseListener(new MouseAdapter() { //采用鼠标监听适配器public void mouseDoubleClick(MouseEvent e) { //监听鼠标双击事件的方法text.setText("文本框中鼠标双击事件发生!"); //在text中显示信息//声明信息对话框对象,并在对话框中显示信息MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.ICON_INFORMATION);dialog.setText("Double click");dialog.setMessage("文本框中鼠标双击事件发生!");dialog.open();}});shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();}}
键盘监听器,监听键盘事件。
package edu.ch4;import org.eclipse.swt.widgets.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.events.*;import org.eclipse.swt.SWT;import org.eclipse.swt.graphics.*;public class Sample4_19 {Text text1,text2;PDF 文件使用 "pdfFactory" 试用版本创建www.fineprint.cnpublic Sample4_19() {Display display = new Display();Shell shell = new Shell(display,SWT.SHELL_TRIM);GridLayout layout=new GridLayout();layout.numColumns=2;shell.setLayout(layout);shell.setText("Event demo");Label label1=new Label(shell,SWT.RIGHT);label1.setText("text1:");text1=new Text(shell,SWT.BORDER|SWT.WRAP);GridData gridData1=new GridData(100,30);text1.setLayoutData(gridData1);text1.addKeyListener(new KeyAdapter(){ //添加按键监听器于text1上public void keyPressed(KeyEvent e) { //监听键盘按键if(e.keyCode==SWT.CR) //当按键为回车键时触发text2.setText(text1.getText());}});Label label2=new Label(shell,SWT.RIGHT);label2.setText("text2:");text2=new Text(shell,SWT.BORDER|SWT.WRAP);GridData gridData2=new GridData(100,30);text2.setLayoutData(gridData2);text2.setEditable(false);text2.setBackground(new Color(display,255,255,255));shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();}public static void main(String[] args) {Sample4_19 s4_19=new Sample4_19();}}
组件选择监听器,监听组件选择事件。
package edu.ch4;import org.eclipse.swt.widgets.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.events.*;import org.eclipse.swt.SWT;public class Sample4_20 {static Display display = new Display();static final Shell shell = new Shell(display,SWT.SHELL_TRIM);public static void main(String[] args) {shell.setText("组件选择事件示例");Button button=new Button(shell,SWT.PUSH);button.setText("请点击我");RowLayout layout=new RowLayout();layout.marginHeight=10;layout.marginWidth=20;shell.setLayout(layout);RowData data=new RowData(80,40);button.setLayoutData(data);button.addSelectionListener(new SelectionAdapter(){public void widgetSelected(SelectionEvent e){MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.ICON_INFORMATION);dialog.setText("组件选择事件");dialog.setMessage("你好,SWT世界!");dialog.open();}});shell.pack();shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();}}
转载地址:http://haqjl.baihongyu.com/