SWT listener

 

这次做的项目中用到了SWT包设计界面,在做的过程中有一些widget的常用的事件处理,整理如下:
为一个widget添加Listener,调用 addListener() .其函数原形:
void addListener(int eventType, Listener listener)
eventType为SWT类定义的事件常量 . 每个常量代表程序中的一个事件,比如:SWT.FocusIn,SWT.MouseDoubleClick,SWT.KeyDown等. 常用的事件类型参照表A。第二个参数Listener 对象可以是一个已经定义好有名字的Listener对象,也可以是匿名类,可以是内部类(inner class)也可以是外部类(outer class),不过大多使用匿名的内部类, 例如为一个按钮添加点击事件的监听器,代码如:

button.addListener(SWT.Selection, new Listener() { 
public void handleEvent(Event e) { 
  switch (e.type) { 
     case SWT.Selection: 
     System.out.println("Button pressed"); 
      break; 
  } 
 } 
}); 

Table A: Event Types

Type

Description

SWT.Activate

Triggered when the widget becomes the active window

SWT.Arm

Triggered when the widget is armed

SWT.Close

Triggered when the widget is closed

SWT.Collapse

Triggered when a tree node is collapsed

SWT.Deactivate

Triggered when the widget is no longer the active window

SWT.DefaultSelection

Triggered when the default selection occurs

SWT.Deiconify

Triggered when the widget is restored from being minimized

SWT.Dispose

Triggered when the widget is disposed

SWT.DragDetect

Triggered when the widget is dragged

SWT.Expand

Triggered when a tree node is expanded

SWT.FocusIn

Triggered when the widget gains focus

SWT.FocusOut

Triggered when the widget loses focus

SWT.HardKeyDown

Triggered when a special hardware key, such as on a Pocket PC device, is pressed

SWT.HardKeyUp

Triggered when a special hardware key, such as on a Pocket PC device, is released

SWT.Help

Triggered when the user requests help

SWT.Hide

Triggered when the widget is hidden

SWT.Iconify

Triggered when the widget is minimized

SWT.KeyDown

Triggered when the user presses a key

SWT.KeyUp

Triggered when the user releases a key

SWT.MenuDetect

Triggered when a menu is selected

SWT.Modify

Triggered when the text of a widget is modified

SWT.MouseDoubleClick

Triggered when the mouse is double-clicked

SWT.MouseDown

Triggered when the mouse button is clicked

SWT.MouseEnter

Triggered when the mouse pointer enters the widget

SWT.MouseExit

Triggered when the mouse pointer exits the widget

SWT.MouseHover

Triggered when the mouse pointer hovers over the widget

SWT.MouseMove

Triggered when the mouse pointer moves through the widget

SWT.MouseUp

Triggered when the mouse button is released

SWT.Move

Triggered when the widget is moved

SWT.None

Null event

SWT.Paint

Triggered when the widget is painted

SWT.Resize

Triggered when the widget is resized

SWT.Selection

Triggered when the widget is selected

SWT.Show

Triggered when the widget is shown

SWT.Traverse

Triggered when the user tabs through the controls

SWT.Verify

Triggered when the text for the widget is about to change, allowing you to veto the change

 SWT提供了很多事件监听器的接口(Interface)和适配器(Adapter),可以为控件添加相应的监听器。

为一个文本框添加按键监听器代码形如:

txtSample.addKeyListener(new KeyAdapter() { 
 public void keyPressed(KeyEvent event) { 
  switch (event.keyCode) { 
     case SWT.KEYPAD_CR: 
     case SWT.CR:
       System.out.println("Enter Key be pressed!");
           break; 
  } 
 } 
}); 

为一个按键添加单击事件监听器,代码形如:

btnSample.addSelectionListener(new SelectionAdapter() { 
 public void widgetSelected(SelectionEvent event) { 
  //在这里添加按钮单击事件的处理代码
 } 
}); 

给文本框添加文本框内容改变的监听器,代码形如:

txtSample.addModifyListener(new ModifyListener() { 
 public void modifyText(ModifyEvent event) { 
  //在这里添加文本框文本内容改变的处理代码
  //一般地,要先移除该文本框的所有监听器以避免不必要的死循环
  //然后在最后把原来的监听器添加回来
 } 
}); 

控件的鼠标双击事件也是经常用到的,其添加监听器的代码形如:

shell.addMouseListener(new MouseListener() { 
 public void mouseDoubleClick(MouseEvent event) { 
  //在这里添加控件双击事件的处理代码  
 } 
}); 

另外,当程序中要处理数据输入时,SWT包提供的输入验证监听器非常有用,它可以避免用户的无效数据输入,其添加监听器的代码形如:

/*Called when user types into text box,but before the text box gets what the user typed*/
txtSample.addVerifyListener(new VerifyListener() { 
 public void verifyText(VerifyEvent event) { 

  //下面是用户输入验证的程序代码示例
  //Assume you don't allow it
  event.doit = false;
  //Get the Character typed
  char myChar = event.character;
  //Allow 0~9
  if ( Character.isDigit(myChar) ) event.doit = true;
  //Allow Backspace
  if ( myChar == '/b' ) event.doit = true;
  else{
    MessageBox msgBox = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
    msgBox.setText( "输入错误!" );
    msgBox.setMessage( "你不能输入除数字和退格键以外的字符!" );
    msgBox.open();
  } 
 } 
}); 

最后再给一个如何处理消息框用户按了哪个键的代码例子:

MessageBox msgBox = new MessageBox( shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION );
msgBox.setText( "消息框的标题" );
msgBox.setMessage( "消息框的提示文字" );
switch( msgBox.open() ){
//msgBox.open()返回所选按钮的常量
  case SWT.YES:
      //程序处理
      break;
  case SWT.NO:
      //程序处理
      break;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值