频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Eclipse开发经典教程:SWT事件

来源: 作者:佚名 出处:巧巧读书 2008-02-19 进入讨论组
上一页 1 2 3 4 5 6 7 

其他常用事件

SWT中为了实现特定的功能,很多组件都提供了特定事件的响应,本节将通过实例介绍HelpListener、VerifyListener和ModifyListener 3个特定的事件监听器。

HelpListener、VerifyListener和ModifyListener监听器的功能

HelpListener监听器通过helpRequested(HelpEvent e)方法响应用户的帮助请求事件,当用户在组件获得焦点后按【F1】键将触发此事件。

VerifyListener监听器通过verifyText(VerifyEvent e)方法响应校验输入事件。此监听器只对文本输入校验,当用户输入了数据后,verifyText方法将通过设置VerifyEvent中的doit属性判断输入是否正确,从而确定修改是否有效。doit属性为true时修改有效,即“e.doit = true;”。

ModifyListener监听器通过modifyText(ModifyEvent e)方法响应文本被修改的事件。此监听器只对文本输入校验。
提示:如果VerifyListener监听器和ModifyListener监听器同时存在的话,会先响应校验输入事件,如果校验成功再响应修改事件。

HelpListener、VerifyListener和ModifyListener监听器实例

在此实例中,用户可以输入华氏温度和摄氏温度,通过监听器判断输入是否正确及计算相应的摄氏温度和华氏温度,另外,还可以按【F1】键获得当前组件的信息,代码如例程13所示。

例程13 MultipleListenersExample.java

public class MultipleListenersExample implements HelpListener, VerifyListener, ModifyListener { private static final double FIVE_NINTHS = 5.0 / 9.0; private static final double NINE_FIFTHS = 9.0 / 5.0; private Text fahrenheit; private Text celsius; private Label help; public void run() { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Temperatures"); createContents(shell); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } private void createContents(Shell shell) { shell.setLayout(new GridLayout(3, true)); new Label(shell, SWT.LEFT).setText("Fahrenheit:"); fahrenheit = new Text(shell, SWT.BORDER); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; fahrenheit.setLayoutData(data); fahrenheit.setData("Type a temperature in Fahrenheit"); // 为华氏温度文本框添加监听器 fahrenheit.addHelpListener(this); fahrenheit.addVerifyListener(this); fahrenheit.addModifyListener(this); new Label(shell, SWT.LEFT).setText("Celsius:"); celsius = new Text(shell, SWT.BORDER); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; celsius.setLayoutData(data); celsius.setData("Type a temperature in Celsius"); //为摄氏温度文本框添加监听器 celsius.addHelpListener(this); celsius.addVerifyListener(this); celsius.addModifyListener(this); help = new Label(shell, SWT.LEFT | SWT.BORDER); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 3; help.setLayoutData(data); } //响应帮助事件 public void helpRequested(HelpEvent event) { help.setText((String) event.widget.getData()); }  //响应校验事件 public void verifyText(VerifyEvent event) { event.doit = false; char myChar = event.character; String text = ((Text) event.widget).getText(); if (myChar == '-' && text.length() == 0) event.doit = true; if (Character.isDigit(myChar)) event.doit = true; if (myChar == '\b') event.doit = true; }  //响应文本修改的事件 public void modifyText(ModifyEvent event) { // 删除监听器,从而在modifyText过程中不会触发事件 celsius.removeVerifyListener(this); celsius.removeModifyListener(this); fahrenheit.removeVerifyListener(this); fahrenheit.removeModifyListener(this); Text text = (Text) event.widget; try { int temp = Integer.parseInt(text.getText()); if (text == fahrenheit) { celsius.setText(String.valueOf((int) (FIVE_NINTHS * (temp - 32)))); } else { fahrenheit.setText(String.valueOf((int) (NINE_FIFTHS * temp + 32))); } } catch (NumberFormatException e) { /* Ignore */ } //添加监听器 celsius.addVerifyListener(this); celsius.addModifyListener(this); fahrenheit.addVerifyListener(this); fahrenheit.addModifyListener(this); } public static void main(String[] args) { new MultipleListenersExample().run(); } }

程序运行效果如图4所示。

Eclipse开发经典教程:SWT事件(图五)
图4 文本监听器


提示:一般来说,监听器都有一个抽象的Adaper类实现监听器的方法,例如FocusAdapter实现了FocusListener的方法(方法为空)。如果读者不想实现监听器的全部方法则可以继承监听器的Adaper类,否则要实现监听器接口的所有方法。

巧 巧 读 书:http://www.qqread.com/java/2008/02/r397063.html 更多文章 更多内容请看Eclipse应用技术ASP.NET教程Wlan组网----家庭专题专题,或进入讨论组讨论。
上一页 1 2 3 4 5 6 7 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章