June 2002
Beginner to intermediate
640 pages
14h 24m
English
We touched on buttons in Chapter 11. They're pretty simple, so we'll cover them here. Import the JFrame, JButton, Frame, and Button classes.
>>> from javax.swing import JFrame, JButton
>>> button = JButton("J Button")
>>> frame = JFrame(visible=1)
>>> frame.title = "JFC"
>>> frame.contentPane.add(button)
>>> frame.pack()
To demonstrate Java event handling we'll create a class that implements ActionListener. Then, using addActionListener, we'll register 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" ... ...
Read now
Unlock full access