Java Reference
In-Depth Information
Figure 4-3.
So what happened? Because EmpFrame set the frame size to 300 by 300 and the button was placed at 350 and
370, the button was not visible. We need a method (setExitButtonLocation) to change the location of the ExitButton
based on the size of each UsefulFrame subclass.
The width and height of a frame are properties (that we have set and reset many times in the examples). If a
property can be set, it can also be retrieved. Can you guess the names of the methods that will return the width and
height of the frame? Our new method, setExitButtonLocation, will retrieve the width and height of the frame and
calculate the exit button's location as 50 pixels from the right side of the frame (the width) and 30 pixels from the
bottom of the frame (the height).
4.
After the UsefulFrame constructor, add the following and then save the source:
public void setExitButtonLocation() {
eb.setLocation( this .getWidth()-50, this .getHeight()-30);
}
Notice that eb's (the exit button's) location is defined with a setLocation method (which eb inherited
from the Button class). Within the setLocation statement, we want to use the UsefulFrame subclass's size to
calculate the location (instead of specifying a fixed location as we did in all past examples). This means that the
setExitButtonLocation method must be executed in the UsefulFrame subclasses (so that this will refer to the subclass).
Search WWH ::




Custom Search