Java Reference
In-Depth Information
Figure 4-23.
Challenge Exercise
The ExitButton class was an example of a useful, specialized GUI component subclass. In this exercise, you will create
another specialized class.
1.
Create a new Java project called CE and a package called c4.
2.
Copy all the classes in Tutorial/c4 and ReviewEx/c4 into CE/c4.
Don't worry about the overwrite message. The ExitButton and UsefulFrame classes were in both packages,
so overwriting one with the other is not a problem here.
Create a new class called HeaderLabel that is a subclass of Label and has all the
characteristics of the headerLbl (which was created in ShipmentFrame).
3.
4.
Change UsefulFrame to include a HeaderLabel component and assign it to variable hl.
5.
In UsefulFrame, create a method called setHeaderLabelLocation that calculates the
horizontal location of the header such that the header is centered regardless of the width
of the frame.
Here's some help with that:
To calculate the horizontal position, subtract the HeaderLabel width (300) from the width of the frame and divide
the result by 2. For instance, if the width of the frame is 400, the starting location is 50 or (400 - 300) / 2. If the width of
the frame was 700 the starting location is 200 or (700 - 300) / 2.
OK, here's a lot of help with that. The actual method should be:
public void setHeaderLabelLocation() {
hl.setLocation(( this .getWidth()-300)/2, 38); }
 
Search WWH ::




Custom Search