## Section4.8Exercises

Fill in the blanks in each of the following sentences:

1. The JButton, JTextField, and JComponent classes are defined in the __________ package.

2. Java GUIs utilize a form of control known as __________ programming.

3. When the user clicks on a program's JButton, an __________ will automatically be generated.

4. Two kinds of objects that generate ActionEvent s are __________ and __________ . JButtons, JTextField s, and JLabel s are all subclasses of __________ .

5. The JFrame class is a subclass of __________ .

6. If a Java class intends to handle ActionEvent s, it must implement the __________ interface.

1. Explain the difference between: An ActionEvent and an ActionListener() method.

2. Look up the documentation for the JButton class on Sun's Web site: http://java.sun.com/j2se/1.5.0/docs/api/. List the signatures of all its constructors.

3. Suppose we want to set the text in our program's JTextField. What method should we use and where is this method defined? (Hint: Look up the documentation for JTextField. If no appropriate method is defined there, see if it is inherited from a superclass.)

4. Does a JButton have an init() method? Explain.

5. Does a JButton have an add() method? Explain.

6. Suppose you have a program containing a JButton named button. Describe what happens, in terms of Java's event handling model, when the user clicks the button.

7. Design and implement a GUI that contains two JButton s, initially labeled, “Me first!” and “Me next!” Each time the user clicks either button, the labels on both buttons should be exchanged. (Hint: You don't need an if-else statement for this problem.)

8. Modify the GUI in the previous exercise so that it contains three JButton s, initially labeled “First,” “Second,” and “Third.” Each time the user clicks one of the buttons, the labels on the buttons should be rotated. Second should get first's label, third should get second's, and first should get third's label.

9. Design and implement a GUI that contains a JTextField and two JButton s, initially labeled “Left” and “Right.” Each time the user clicks a button, display its label in the JTextField. A JButton()'s label can be gotten with the getText() method.

10. You can change the size of a JFrame by using the setSize(int h, int v) method, where h and v give its horizontal and vertical dimensions pixels. Write a GUI application that contains two JButton s, labeled “Big” and “Small.” Whenever the user clicks on small, set the JFrame's dimensions to 200 × 100, and whenever the user clicks on big, set the dimensions to 300 × 200.

11. Rewrite your solution to the previous exercise so that it uses a single button whose label is toggled appropriately each time it is clicked. Obviously, when the JButton is labeled “Big,” clicking it should give the JFrame its big dimensions.

12. Challenge: Design and write a Java GUI application that allows the user to change the JFrame's background color to one of three choices, indicated by buttons. Like all other Java Component s, JFrame's have an associated background color, which can be set by the following commands:

setBackground(Color.red);
setBackground(Color.yellow);


The setBackground() method is defined in the Component class, and 13 primary colors—black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow—are defined in the java.awt.Color class.