Java Reference
In-Depth Information
This statement defines a constructor method. The primary purpose of the constructor is to "initialize" the object
or said another way “to make the object useful”. For EmpFrame, this means adding source code to the constructor to
define the frame and its component's properties.
Notice that the constructor expects an Employee object. As mentioned, we will be changing the Employee class
to hold more employee information. Passing an object, instead of individual variables, is an easy way to transfer a lot
of information between two classes.
3.
Inside the constructor, enter the following statement:
empNameLbl.setBounds(100, 150, 150, 20);
Notice that the length of the label was set to 150 so that all the text will be displayed. The label was also centered
on the frame (100 pixels from the left and 150 from the top).
4.
Add the following after the setBounds statement:
this .add(empNameLbl);
Adding the label to the frame was the last step we needed to perform. In our earlier example, the label was added
to the empFrame object with the following statement:
empFrame.add(empNameLbl);
In this example, however, we do not have a frame object to manipulate. We must add the label to the EmpFrame
class we are defining. The keyword this is used to refer to the current object/class. For example, notice that the syntax
to add the labels was the same except we substituted this for empFrame.
You have created and added a label to EmpFrame.
The following summarizes how to create a label and which steps (that you just performed) did the required
functions:
A.
Import the java.awt.Label class—step 1
Create a Label object and variable and assign the object to the variable—step 2
B.
C.
Define the label text—step 2
D.
Define the label size—step 4
E.
Define the label location—step 4
F.
Add the label to the frame—step 5
Tutorial: Defining a Functional Frame
To review, we still need to perform the following steps to create a functioning frame:
A.
Define a title
B.
Set layout property to null
C.
Define the size
D.
Define the location
E.
Make the frame visible
 
Search WWH ::




Custom Search