Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 4.8 Exercises

Exercises IO Exercises

Fill in the blank.

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, JTextFields, and JLabels 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.

7.

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

9.

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.

10.

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

11.

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

12.

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.

13.

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.

14.

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.

15.

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.

16.

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.

17.

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.

18.

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.
You have attempted of activities on this page.