博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java SWT事件
阅读量:6932 次
发布时间:2019-06-27

本文共 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/

你可能感兴趣的文章
RHEL 5服务篇—使用Apache搭建web服务(四)部署AWStats网站分析系统
查看>>
SQL Serer闩锁 和 闩锁超时故障排除
查看>>
Logparser 分析 Exchange 日志文件
查看>>
KDE与GNOME的起源与发展
查看>>
Linux下Java开发环境的搭建Tomcat6+jdk6+eclipse3.5.2+Myeclipse9.0+mysql5.1.47
查看>>
部署Nagios监控系统(一)
查看>>
银行程序代发工资的方法
查看>>
常见拒绝服务攻击行为特征与防御方法
查看>>
【游戏开发备注之二】配置Xcode版本控制SVN详细步骤内含部分问题解决方案
查看>>
JAVA 和.NET在安全功能的比较
查看>>
从91移动应用发展趋势报告看国内应用现状
查看>>
jps could not synchronize with target
查看>>
Android系统在新进程中启动自定义服务过程(startService)的原理分析 (下)
查看>>
[RHEL5企业级Linux服务攻略]--第3季 DHCP服务全攻略
查看>>
《Pro ASP.NET MVC 3 Framework》学习笔记之五【依赖注入及ninject工具使用】
查看>>
综合应用WPF/WCF/WF/LINQ之三:采用用代码创建的方式实现CheckListBox的CustomControl
查看>>
【原创】用MySQL 生成随机密码-增加大写处理
查看>>
读源码Apache-commons-lang3-3.1(三)
查看>>
C#复制、粘贴文本信息到剪贴板
查看>>
单IP无TMG拓扑Lync Server 2013:边缘服务器
查看>>