class SimpleFrame extends DecoratedFrame {
Button buttonOK = new Button();
Button buttonCancel = new Button();
void buttonOK_actionPerformed(ActionEvent e) {
System.out.println("OK clicked");
}
void buttonCancel_actionPerformed(ActionEvent e) {
System.out.println("Cancel clicked");
}
void jbInit() throws Exception{
buttonOK.setLabel("OK");
buttonOK.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
buttonOK_actionPerformed(e);
}
}
);
buttonCancel.setLabel("Cancel");
buttonCancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonCancel_actionPerformed(e);
}
}
);
this.add(buttonOK, BorderLayout.NORTH);
this.add(buttonCancel, BorderLayout.SOUTH);
}
}
Ecco la versione WFC che utilizza i delegati per connettere gli event handlers.
Si noti l'instanziazione del delegato EventHandler in the initForm method
The WFC version, which uses delegates to connect event handlers, is below. Note the instantiation of
EventHandler delegates in the initForm method. These delegates encapsulate the actual event handlers. When
the invoke method of a delegate is called, the method it encapsulates is called. For event handlers, this occurs
when the event source raises the event. The code in the example was generated using Microsoft Visual J++ 6.0.
Other design patterns are possible with the delegate-based model, but we believe that developers and tools
vendors will favor the approach used in the example. class SimpleForm extends Form
{
Button buttonOK = new Button();
Button buttonCancel = new Button();
void buttonOK_click(Object sender, Event e){
System.out.println("Clicked OK");
}
void buttonCancel_click(Object sender, Event e){
System.out.println("Clicked Cancel");
}
void initForm(){
buttonOK.setText("OK");
buttonOK.addOnClick(new EventHandler(this.buttonOK_click));
buttonCancel.setText("Cancel");
buttonCancel.addOnClick(new EventHandler(this.buttonCancel_click));
this.setNewControls(new Control[] {buttonCancel, buttonOK});
}
}
