Page 721 - Introduction to Programming with Java: A Problem Solving Approach
P. 721
Summary
• The JFrame class should be used as the superclass for most of your GUI application windows.
• The JFrame class implements all the standard window features such as a border, a title bar, a
minimize button, a close-window button (the “X”), the ability to resize the window, and so on.
• JLabel is a read-only component; the user simply reads the label’s message.
• The JTextField component allows the user to enter text into a text box.
• When the user interacts with a component (e.g., when the user clicks a button or presses enter while in a
text box), the component fires an event.
• If a component has a listener attached to it, the fired event is “heard” by the listener and consequently
handled by the listener.
• A listener handles an event by executing its actionPerformed event-handler method.
• Listeners often are implemented with the ActionListener interface. An interface is a class-like en-
tity whose methods are all empty. If a programmer uses an interface to derive a new class, the compiler
requires the new class to implement methods for all of the interface’s methods.
• If a class is limited in its scope such that it is only needed by one other class, then you should define the
class as an inner class (a class inside of another class).
• An anonymous inner class is an inner class without a name.
• To display a simple window with a message, call JOptionPane’s showMessageDialog method.
• To identify the component whose event was fired, use the actionPerformed method’s
ActionEvent parameter to call getSource or getActionCommand.
• To adjust a GUI component’s text color, call setForeground. To adjust the color behind the text,
Apago PDF Enhancer
call setBackground.
• To adjust a window’s background color, call the content pane’s setBackground method.
• To detect and handle mouse events, use the MouseAdapter and MouseMotionAdapter classes,
which implement the MouseListener and MouseMotionListener interfaces, respectively. Review Questions
§16.2 Event-Driven Programming Basics 1. What is a listener?
2. What is an event handler?
§16.3 A Simple Window Program
3. Write a statement that adds functionality to a program’s close-window button such that when the close-
window button is clicked, it causes the program to terminate.
§16.4 JFrame Class
4. What is the name of the superclass for classes that contain components?
§16.5 Java Components
5. What package are JButton and many other J-prefixed components defined in?
§16.6 JLabel Component
6. Provide an initialization statement that declares a JLabel reference variable named hello and assigns “Hello World” to the reference variable.
Review Questions 687