Java Reference
In-Depth Information
As mentioned in the first chapter, the String class has all sorts of shortcuts that other classes do not. The Label
class (like most classes) has “setter” and “getter” methods to access and change an object property , like a label's text.
Of course, this begs the question, what is a property? A property is a private variable that represents some aspect of
a class that the class user (called the client) should have control over—like the size of a label or the text that appears
on the label. However, direct access to the variable that controls a property could prove disastrous. For instance, what
do you think would happen if a programmer created a label and then specified the size as 16 feet? That's right, the
biggest, fattest error you ever saw, and the JVM would end the entire application.
Because of this, most setter methods check the supplied property values and ensure it is valid. If a value is invalid,
the setter generates an exception that the application/client (i.e., the user of the setter) should “catch.” Catching the
exception stops the JVM from ending the application. However, we are getting several chapters ahead of ourselves. For
now, accept that you will use (and eventually create) getter and setter methods to access and control object properties.
With labels, the programmer usually specifies the text, size, and location properties. There are three setter
methods to do this—setText, setSize, and setLocation. However, we can define the text when the label is created
and there is another method called setBounds that allows the programmer to define both the size and location. For
instance, the following:
Label empNameLbl = new Label("My First Label");
empNameLbl.setBounds(100, 200, 75, 20);
will create a Label object with the text “My First Label” and position the start of the empNameLbl 100 pixels from the
left and 200 pixels from the top of the upper, left corner of the container to which it belongs. Lastly, the label will be
75 pixels wide and 20 pixels high. You may be asking, “How big is a pixel?” The size of a pixel depends on the
computer screen's resolution setting. Earlier, we showed that many aspects of Windows' appearance are controllable.
One of these aspects is the number of pixels that will be displayed on the screen (i.e., the resolution). The screens
seen in all of the previous figures are set to 1024 by 768. Contrary to intuition, fewer pixels mean the GUI components
will appear larger. For instance, if we changed the screen resolution to 800 × 600, one pixel would take up more of the
screen; therefore, a label would look larger.
We have a label. Now we need a frame to put it on. A frame has a white content area, surrounded by a border
and a title bar across the top. In Windows 7, the border and title will be light blue. As with the label, we need to create
the Frame object and set several of its properties. Let's explore the statements needed to create a basic frame and add
empNameLbl to the frame.
Frame empFrame = new Frame("My First Frame");
The first statement creates a Frame object and defines title text (“My First Frame”) for the frame. A Frame variable
named empFrame is created and the Frame object is assigned to empFrame. Notice that the syntax is the same as
creating a String or Label object.
empFrame.setBounds(10,10,300,300);
This statement sets the location and size of the frame. As with a label, the first two numbers (10, 10) control
the number of pixels from the left and top of the screen where the upper left corner of the frame will be placed. The
second set of numbers (300, 300) defines the width and height of the frame (in pixels).
empFrame.setLayout( null );
This third statement sets the frame's layout property to null. Layouts are predefined formats that control where
visual components will appear in a container. Layouts make it simpler to add and format components but impose limits
on the programmer's control of the components, so we will “turn off ” the property. When there is no layout defined, the
programmer must define each component's location (which we already did when we defined the empNameLbl).
empFrame.add(empNameLbl);
Search WWH ::




Custom Search