Java Reference
In-Depth Information
Lab 13.1: Event Handling
The purpose of this lab is to become familiar with handling the events of
GUI components. You are going to add the necessary event handlers so
that your Calculator from Lab 12.1 works.
1. Write a class named CalculatorListener that implements ActionLis-
tener. This class is going to handle the ActionEvent from all 16 JBut-
tons in your calculator.
2. Add a field of type JTextField and a constructor that has a JTextField
parameter. Assign the field to the parameter within this constructor.
3. Your actionPerformed() method is going to perform the arithmetic
of the calculator. Use if/else statements to determine which button
was clicked, and include all the necessary logic for your Calcula-
torListener class so that the calculator functions properly.
You should see a fully functional calculator. Be sure to perform some
quality assurance on your calculator, testing to make sure everything is
working properly.
Lab 13.2: Handling the Instant
Message Events
In this lab, you will start working on the event handling for the instant
message program started in Chapter 12. This lab is a continuation of Lab
12.3, and you will add some event handling to your InstantMessageFrame.
1. Write a class named IMHandler that implements ActionListener.
This class will handle the events from the Send button and text field
of your InstantMessageFrame.
2. Add a field of type JTextField and JList, and initialize each field
using a constructor.
3. The actionPerformed() method will be invoked when the user either
clicks the Send button or presses the Enter key in the text field. In
either case, get the text from the text field and print it out at the
command prompt using System.out.println() for each friend in the
JList. For example, if the text is “Hello” and there are four friends in
the list, the output should say “Hello” four times, with each friend's
name being displayed next to “Hello.”
Search WWH ::




Custom Search