Saturday 15 March 2014

Java link event to event handler -


I am working on a GUI library for my game, and I try to find a good way to link to events I am doing a GUI element (for example, a click-event from a button) for an event handler.

Say that I have a button named Mibton, I will use the following code to set it (incomplete, only for display):

  Button MyButton = New button (); MyButton.SetParent (MyContainer); MyButton.SetText ("Text inside my button!"); MyButton.SetTextColor (Color.BLACK);   

Now this code will be called in one of my games, and what I would like to do is something like this:

  MyButton.OnClick (MyButtonClickEvent); Public Zero MyButtonClickEvent (EventArgs event) {}   

The compositions of my gamestates and containers and elements are as follows:

GameEngen-> GameState-> Container-> Element

What would be the closest solution to this? thank you in advanced.

Java does not have methods such as first-rank functions, that is, language is not such a method Provide references to which you can do in other languages ​​too. Your best bet is to use a reflection of the kind mentioned in your question.

  import java.lang.NoSuchMethodException; Import java.lang.reflect.InvocationTargetException; Import java.lang.reflect.Method; Import java.util.Set; Public category MyButton {Private Object onClickObject; Private method onClickMethod; Public Zero OnClick throws NoSuchMethodException (object object, string name name) {OnClick (object, object.getClass (). GetMethod (methodName)); } Public Zero OnClick (Object Object, Method Method) {this.onClickObject = object; This.onClickMethod = method; } // MyButton clicks this method every time the button is clicked, // in order to inform the external event handler about the command throws safe void onClick () IllegalAccessException, InvocationTargetException {onClickMethod. Invoke (onClickObject); }}   

But keep in mind that because methods are not a first-class citizen, in the above Java there is no authentic way of implementing event listeners. . Instead, you must define an interface with the Java-way callback method, perhaps it is as follows:

  Public Interface Button Listener {Public Zero OnClick (); }   

(It is assumed that you do not have to give any parameters to the event handler. Generally, this can not be considered, thus a button listener , You also have a button event which encapsulates the parameter and passes the method (s) defined in the interface.)

Then if you type a class Whenever a certain button is clicked, apply that class to ButtonListener In return, MyButton class must provide a method to register listeners:

  public MyButton {protected list & lt; ButtonListener & gt; ButtonListeners; Public Zero addButtonListener (Button Listener Listener) {...} Public Zero Delete ButtonListener (Button Listener Listener) {...} Safe void fireButtonEvent () {...}}   

Sure you have seen that pattern in the Java standard classroom library, especially in java.awt and javax.swing - for example see java.awt .event.ActionListener , which uses the AWT button for events.

No comments:

Post a Comment