Section 4.8 Exercises
Fill in the blanks in each of the following sentences:
- The
JButton
,JTextField
, andJComponent
classes are defined in the__________
package. - Java GUIs utilize a form of control known as
__________
programming. - When the user clicks on a program’s
JButton
, an__________
will automatically be generated. - Two kinds of objects that generate
ActionEvent
s are__________
and__________
. JButtons,JTextField
s, andJLabel
s are all subclasses of__________
. - The
JFrame
class is a subclass of__________
. - If a Java class intends to handle
ActionEvent
s, it must implement the__________
interface.
- Explain the difference between: An
ActionEvent
and anActionListener()
method. - 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. - 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 forJTextField
. If no appropriate method is defined there, see if it is inherited from a superclass.) - Does a
JButton
have aninit()
method? Explain. - Does a
JButton
have anadd()
method? Explain. - Suppose you have a program containing a
JButton
namedbutton
. 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
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.) - 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. - Design and implement a GUI that contains a
JTextField
and twoJButton
s, initially labeled “Left” and “Right.” Each time the user clicks a button, display its label in theJTextField
. AJButton()
’s label can be gotten with thegetText()
method. - You can change the size of a
JFrame
by using thesetSize(int h, int v)
method, where h and v give its horizontal and vertical dimensions pixels. Write a GUI application that contains twoJButton
s, labeled “Big” and “Small.” Whenever the user clicks on small, set theJFrame's
dimensions 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
JButton
is labeled “Big,” clicking it should give theJFrame
its 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 JavaComponent
s,JFrame
’s have an associated background color, which can be set by the following commands:setBackground(Color.red); setBackground(Color.yellow);
ThesetBackground()
method is defined in theComponent
class, and 13 primary colors—black
,blue
,cyan
,darkGray
,gray
,green
,lightGray
,magenta
,orange
,pink
,red
,white
,yellow
—are defined in thejava.awt.Color
class.
You have attempted of activities on this page.