Java Reference
In-Depth Information
Create an instance of class JFrame and assign it to variable myWindow . (Read the
beginning of Sec. 1.4 if you need help with this.) Now show the window, set the
title to "My Window" , and evaluate myWindow.getWidth() .
Execute these statements:
JFrame myOtherWindow= myWindow;
myOtherWindow.setSize(444, 777);
Evaluate myWindow.getWidth() again. You changed myOtherWindow ; why did
myWindow 's width also change?
SR2. myWindow.getWidth() and myWindow.getHeight() get the width and
height of the window. myWindow.setSize( expression 1 , expression 2 ); sets the
size of the window. Figure out a single Java statement that will swap the height
and width of the window to which myWindow refers.
SR3. Write a custom JFrame class called ResizerFrame that has a single method
swapDimensions() that sets the height to the width and the width to the height.
(You can base it on the code in OurFrame in Fig. 1.10 a few pages back.)
Compile it and then create an instance of ResizerFrame and assign it to a vari-
able rf . Now call rf.swapDimensions() and make sure it works.
1.5
Static components
If a method does not access fields of a class or other instance methods, there is
no need to place it in each folder of the class. An example of this is method sum ,
below, whose body references only parameters a , b , and c :
/** = sum of a , b , and c */
public static int sum( int a, int b, int c) {
return a+b+c;
}
When writing such methods, we use keyword static . The presence of this key-
word indicates that this component does not belong in each folder of the class.
Instead, there is only one copy of the component, and it is stored right in the file
a1
JFrame
show() hide()
setLocation(int,int) etc.
ours a1
OurFrame
setTitleToOrigin()
getPreviousTitle()
previousTitle
""
Figure 1.11:
Revised instance ours of class OurFrame , with field previousTitle
Search WWH ::




Custom Search