Java Reference
In-Depth Information
JFrame Classes
When we say that a class is a JFrame class we mean the class is a descendent class of the
class JFrame . For example, the class FirstWindow in Display 17.4 is a JFrame class. When
we say an object is a JFrame , we mean that it is an object of some JFrame class.
Self-Test Exercises
10. Change the program in Display 17.4 so that the title of the JFrame is not set by
the method setTitle but is instead set by the call to the base class constructor.
Hint: Recall Self-Test Exercise 9.
11. Change the program in Display 17.4 so that there are two ways to end the
GUI program: The program can be ended by either clicking the "Click to
end program." button or clicking the close-window button.
Labels
We have seen how to add a button to a JFrame . If you want to add some text to your
JFrame , use a label instead of a button. A label is an object of the class JLabel . A label
is little more than a line of text. The text for the label is given as an argument to the
JLabel constructor as follows:
JLabel greeting = new JLabel("Hello");
label
JLabel
The label greeting can then be added to a JFrame just as a button is added. For exam-
ple, the following might appear in a constructor for a derived class of JFrame :
JLabel greeting = new JLabel("Hello");
add(greeting);
The next Programming Example includes a label in a JFrame GUI.
The JLabel Class
An object of the class JLabel is little more than one line of text that can be added to a
JFrame (or, as we will see, added to certain other objects).
EXAMPLE (INSIDE A CONSTRUCTOR FOR A DERIVED CLASS OF JFrame )
JLabel myLabel = new JLabel("Hi Mom!");
add(myLabel);
Search WWH ::




Custom Search