Section 4.8 Exercises
Fill in the blanks in each of the following sentences:
JComponentclasses are defined in the
- Java GUIs utilize a form of control known as
- When the user clicks on a program’s
__________will automatically be generated.
- Two kinds of objects that generate
JLabels are all subclasses of
JFrameclass is a subclass of
- If a Java class intends to handle
ActionEvents, it must implement the
- Explain the difference between: An
- Look up the documentation for the
JButtonclass on Sun’s Web site: http://java.sun.com/j2se/1.5.0/docs/api/. List the signatures of all its constructors.
- 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.)
- Does a
- Does a
- Suppose you have a program containing a
button. Describe what happens, in terms of Java’s event handling model, when the user clicks the button.
- Design and implement a GUI that contains two
JButtons, 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.)
- Modify the GUI in the previous exercise so that it contains three
JButtons, 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.
- Design and implement a GUI that contains a
JButtons, initially labeled “Left” and “Right.” Each time the user clicks a button, display its label in the
JButton()’s label can be gotten with the
- You can change the size of a
JFrameby 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
JButtons, labeled “Big” and “Small.” Whenever the user clicks on small, set the
JFrame'sdimensions to 200 × 100, and whenever the user clicks on big, set the dimensions to 300 × 200.
- 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
JButtonis labeled “Big,” clicking it should give the
JFrameits big dimensions.
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
JFrame’s have an associated background color, which can be set by the following commands:
setBackground()method is defined in the
Componentclass, and 13 primary colors—
yellow—are defined in the
You have attempted of activities on this page.