Java Reference
In-Depth Information
Class LabelFrame (Fig. 12.6) extends JFrame to inherit the features of a window.
We'll use an instance of class LabelFrame to display a window containing three JLabel s.
Lines 12-14 declare the three JLabel instance variables that are instantiated in the Label-
Frame constructor (lines 17-41). Typically, the JFrame subclass's constructor builds the
GUI that's displayed in the window when the application executes. Line 19 invokes super-
class JFrame 's constructor with the argument "Testing JLabel" . JFrame 's constructor
uses this String as the text in the window's title bar.
Specifying the Layout
When building a GUI, you must attach each GUI component to a container, such as a
window created with a JFrame . Also, you typically must decide where to position each GUI
component—known as specifying the layout . Java provides several layout managers that
can help you position components, as you'll learn later in this chapter and in Chapter 22.
Many IDEs provide GUI design tools in which you can specify components' exact
sizes and locations in a visual manner by using the mouse; then the IDE will generate the
GUI code for you. Such IDEs can greatly simplify GUI creation.
To ensure that our GUIs can be used with any IDE, we did not use an IDE to create
the GUI code. We use Java's layout managers to size and position components. With the
FlowLayout layout manager, components are placed in a container from left to right in the
order in which they're added. When no more components can fit on the current line, they
continue to display left to right on the next line. If the container is resized , a FlowLayout
reflows the components, possibly with fewer or more rows based on the new container
width. Every container has a default layout , which we're changing for LabelFrame to a
FlowLayout (line 20). Method setLayout is inherited into class LabelFrame indirectly
from class Container . The argument to the method must be an object of a class that imple-
ments the LayoutManager interface (e.g., FlowLayout ). Line 20 creates a new FlowLayout
object and passes its reference as the argument to setLayout .
Creating and Attaching label1
Now that we've specified the window's layout, we can begin creating and attaching GUI
components to the window. Line 23 creates a JLabel object and passes "Label with text"
to the constructor. The JLabel displays this text on the screen. Line 24 uses method set-
ToolTipText (inherited by JLabel from JComponent ) to specify the tool tip that's dis-
played when the user positions the mouse cursor over the JLabel in the GUI. You can see
a sample tool tip in the second screen capture of Fig. 12.7. When you execute this appli-
cation, hover the mouse pointer over each JLabel to see its tool tip. Line 25 (Fig. 12.6)
attaches label1 to the LabelFrame by passing label1 to the add method, which is inher-
ited indirectly from class Container .
Common Programming Error 12.1
If you do not explicitly add a GUI component to a container, the GUI component will
not be displayed when the container appears on the screen.
Look-and-Feel Observation 12.7
Use tool tips to add descriptive text to your GUI components. This text helps the user de-
termine the GUI component's purpose in the user interface.
 
Search WWH ::




Custom Search