Java Reference
In-Depth Information
For example, the investment viewer has a single input: the interest rate. Other
programs may have different inputs, such as amounts of money, product quantities,
and so on.
Step 3 For each action, enumerate the outputs that you need to show.
The investment viewer has a single output: the current balance. Other programs
may show different quantities, messages, and so on.
Step 4 Supply the user interface components.
Right now, you need to use buttons for actions, text fields for inputs, and labels for
outputs. In Chapter 18 , you will see many more user-interface components that can
be used for actions and inputs. In Chapter 3 , you learned how to implement your
own components to produce graphical output, such as charts or drawings.
Add the required buttons, text fields, and other components to a frame. In this
chapter, you have seen how to lay out very simple user interfaces, by adding all
components to a single panel and adding the panel to the frame. Chapter 18 shows
you how you can achieve more complex layouts.
Step 5 Supply event handler classes.
For each button, you need to add an object of a listener class. The listener classes
must implement the ActionListener interface. Supply a class for each action
(or group of related actions), and put the instructions for the action in the
actionPerformed method.
class Button1Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// button1 action goes here
. . .
}
}
486
487
Remember to declare any local variables accessed by the listener methods as
final .
Step 6 Make listener objects and attach them to the event sources.
Search WWH ::




Custom Search