Java Reference
In-Depth Information
window.setTitle("another title");
We give the specifications of some of the method of class JFrame in Fig. 1.6.
We encourage you to experiment with class
JFrame
in your IDE. Create a
JFrame
and assign its name to a variable such as
window
. Then, practice using
the methods of Fig. 1.6. For example, you can find out the width of window
win-
dow
by evaluating the call
window.getWidth()
and set the position of the win-
dow to (
200
,
300)
by having this statement executed:
window.setLocation(200, 300);
As a more complex example, we develop statements that will change the
height of window
window
to its width, thus making the window a square. To do
this, we do two things:
1. Obtain the width of the window and store it in variable
jfWidth
;
2. Change the height of the window to
jfWidth
.
When determining how to perform a programming task, it helps to write it
down in English as a sequence of steps to be performed, as above. Then, we can
figure out how to write each of the steps in Java. Separating
what
to do from
how
to do it is an example of
separation of concerns
, which is an important strategy
in any problem-solving task. Use it consciously to focus on one thing at a time.
We are also using a methodology called
stepwise refinement
. The develop-
ment consists of a series of steps. At each step, we “refine” part of the task into
more detailed steps, some of them in English and some of them in Java, until the
whole task has been written in Java. We discuss stepwise refinement in Sec. 2.5.
We return to the task of refining the two steps given above into Java. To see
how to obtain the width of the window (step 1), we look through the list of meth-
ods described in Fig. 1.6. There it is: method
getWidth
will do the trick. So, we
can write a statement:
window.show();
Show window
window
.
window.hide();
Hide window
window
.
window.getHeight()
=
height of window
window
, in pixels
window.getWidth()
=
width of window
window
, in pixels
window.setSize(w,h);
Set width and height of window
window
to
w
and
h
window.getX()
=
x
-
coordinate of top left corner of window
window
window.getY()
=
y
-
coordinate of top left corner of window
window
window.setLocation(x,y);
Set
x
-
and
y
-coordinates of top left corner of
window
to
x
,
y
window.getTitle()
=
the title of window
window
(in the title bar)
Set the title of window
window
to
s
(a
String
).
window.setTitle(s);
=
“window
window
can be resized by dragging it”
window.isResizable()
If
b
is
true
(
false
) make window resizable (not resizable).
window.setResizable(b);
Figure 1.6:
Calls on some methods in an instance
window
of a
JFrame
Search WWH ::
Custom Search