Java Reference
In-Depth Information
Components are positioned in a frame or container by
objects called layout managers, such as BorderLayout ,
FlowLayout , and GridLayout . The features of the lay-
out managers can be combined by nesting them in differ-
ent containers to form a composite layout.
the ActionListener interface. You can also respond to
MouseEvents by writing a class that extends the
MouseInputAdapter class.
To draw lines and shapes, you must write a class that
extends JPanel and write a method called
paintComponent .
Java generates special objects called events when the user
interacts with onscreen graphical components. To write an
interactive GUI, you must write responses to these events.
GUIs can be animated using objects called timers that
cause events to fire on an action listener at regular
intervals.
The most common type of event is an ActionEvent ,
which you can handle by writing a class that implements
Self-Check Problems
Section 14.1: GUI Basics
1. What are the three new packages introduced in this section that you must import when you write graphical pro-
grams? Write the import statements that are necessary in a program that uses these classes.
2. Write Java code to pop up an option pane asking the user for his or her age. If the user types a number less than 40,
respond with a message box saying that he or she is young. Otherwise, tease the user for being old. Here is an example:
3. What is a component? How is a frame different from other components?
4. Name two properties of frames. Give an example piece of code that creates a new frame and sets these two proper-
ties to have new values of your choice.
5. Identify the Java class used to represent each of the following graphical components.
a.
 
 
Search WWH ::




Custom Search