0

Forgive me if this question has been asked and answered, as I have been unable to find it if it has.

I can find several examples like

button.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        ... stuff ...
    }
});

I'm trying to understand how to implement a button click handler using the following structure.

public class myClass implements EntryPoint {
    final Button MyButton = new Button("text");
        :
        :
    void onClickMyButton(???) {
            ... stuff ...
    }
        :
        :
}

To "me", this structure is more easily read and is just my preference in coding style. But I don't know how to implement it.

I'm using Eclipse and GWT for a Java Web App. Any help would be appreciated.

4

1 回答 1

1

Either

public class myClass implements EntryPoint, ClickHandler {
    final Button myButton = new Button("text");
        :
        :
    myButton.addClickHandler(this);


    @Override
    void onClick(ClickEvent event) {
            ... stuff ...
    }
}

or

public class myClass implements EntryPoint {
    final Button myButton = new Button("text");
        :
        :
    myButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            onClickMyButton(event);
        }
    });

    private void onClickMyButton(ClickEvent event) {
            ... stuff ...
    }
}

The second one is much cleaner, and allows handling several buttons with separate methods.

于 2013-08-19T21:40:00.393 回答