1.匿名内部类写法
button.addSelectionListener(new SelectionAdapter(){
@Override
public void widgetSelected(SelectionEvent e){
.....
}
});
释:当button被单击时,底层就会检查button是否注册了相对就的监听器,如果有,底层就会调用监听器的相应事件方法(如widgetSelected)。观察者
2.命名内部类写法
class MyEvent{
Public static void main(String[] args){
.....
Button.addSelectionListener (new ButtonSelectionListener());//按钮
.....
}
//定义一个内部类
Private static class ButtonSelectionListener extends SelectionAdapter{
@Override
Public void widgetSelected(SelectionEvent e){
.....
}
}
}
3.外部类写法
//文件1:ButtonEvent.java
Public class {
ButtonEvent
Public static void main(String[] args){
……
Button.addSelectionListener(new ButtonSelectionListener());
……
}
}
//文件2:ButtonSelectionListener.java
Public class ButtonSelectionListener extends SelectionAdapter{
public void widgetSelected(SelectionEvent e){
......
}
}
4.实现监听接口的写法
Public class HelloWorld extends SelectionAdapter{//implements SelectionListener
Public static void main(String[] args){
……
Button.addSelectionListener(new HelloWorld());
//此处HelloWorld为SelectionAdapter的子类
……
}
@Override
Public void widgetSelected(SelectionEvent e){
.....
}
}