Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^386) | Event-Driven Input and Output
into the pane and register a listener with the button. First we consider how to add a button
to a pane. The process is very much like adding a label.
1.Declare a variable of the JButtonclass.
2.Instantiate a JButtonobject and assign its address to the variable.
3.Add the object to the frame’s content pane using the addmethod.
An example JButtondeclaration is
JButton done; // Declare a JButton variable called done
Event Listener Object
Button
event
firing Handler
call
Event Handler
public void actionPerformed(ActionEvent event)
class ButtonListener
{... }
myButton
Registering an instance of the
ButtonListener with the button
object that generates the event.
The point of execution when
the event occurred, and where
execution resumes when the
eventhandler returns
Program on Hold
public class PrintName...
public static void main(... )
{
my Button.addActionListener(new ButtonListener());
.
.
.
}
Figure 8.4 Event Handling

Free download pdf