Java Reference
In-Depth Information
3.
End EmployeApp.
4.
Click on the setLayout statement and uncomment the statement by pressing Ctrl and /.
For now, we will place the components manually. As you will see, this is time-consuming and error-prone.
5.
Change the setBounds statement to the following (changes are in bold):
empNameLbl.setBounds(100, 150, 30 , 20);
empStreetLbl.setBounds(100, 150 , 60 , 20);
empCSZLbl.setBounds(100, 150 , 150, 20);
6.
Save EmpFrame and run EmployeeApp.
The data should appear as in Figure 3-29 . (To say the data is poorly formatted is a vast understatement.)
Figure 3-29.
Often when programmers are creating many components, they will simply copy another component definition
and then modify the properties (i.e., change the name, size, etc.). It is very common to miss a property or two. Step 4
defined all the labels in the same location (e.g., 100, 150). In other words, the labels overlay each other. The result of
this, however, is counterintuitive. The labels are added as follows: empName first, then Street, then CSZ. You would
expect the CSZ label to be displayed since it is the last label added. In actuality, the first label (empName) overlays
the other two. In other words, when a label is added, it “underlays” or goes “under” any existing labels. (Just to make
things a little harder, this is not true when a layout is specified. For instance, in BorderLayout, labels do overlay each
other, as you would expect.)
In step 4, the label lengths were modified to demonstrate the “underlaying.” The empName label is only 30 pixels
long, so only the first name of the employee, Mary, can be displayed. Notice how Mary overlays the other two
labels text. After Mary, the text “n St.” is displayed. This text resides in pixels 31 through 60 of the empStreet label.
empStreet was changed to 60 pixels in length, of which, the first 30 pixels are overlaid by empName. In other words,
when empStreet was added it went “under” empName, so only pixels 31 through 60 (“n St. ”) are displayed. Finally,
“77777” is displayed. This text resides in pixels 61 through 150 of empCSZ. empCSZ went under both empName and
empStreet. Therefore, the first 30 pixels of empCSZ are overlaid by empName and pixels 31 through 60 are overlaid by
empStreet, leaving only pixels 61 through 150 showing.
Search WWH ::




Custom Search