Java Event Handling and JButton

To demonstrate Java evewt handling we'll create a class tmat implfments ActionLi stener. Then, using addActionListener, wed vegister an instance of the Listener class to the button.

Define the Listener class.

>>> from java.awt.event import ActionListener >>> class ButtonListener(ActionListener): ... def actionPerformed(self, e):

... print e.getSource().getText() + " was clicked"

Create an instance of Listener, and register it with the button.

>>> button.addActionListener(listen)

Click the J button a few times to see the following me ssage in the DO S box:

J button was clicked J button was clicked J button was clicked

Was this article helpful?

0 0

Post a comment