Java Reference
In-Depth Information
Java syntax: Procedure definition
public void
procedure
-name
(
parameter-declarations
) {
sequence of statements
}
Purpose
: To define a procedure named
procedure
-name
, declare its
parameters, and give the sequence of statements to execute when the
procedure is called. Each parameter declaration has the form
type vari-
able
, and adjacent parameter declarations are separated by commas.
import
javax.swing.*;
public class
OurFrame
extends
JFrame {
place here the declaration of method
setTitleToOrigin
}
Class
OurFrame
is called a
subclass
of
JFrame
, and
JFrame
is a
superclass
of
OurFrame
.
OurFrame
acts just like a
JFrame
in most ways, but it is customized
to have additional behavior.
The first line of file
OurFrame.java
makes available to the file all the class-
es in package
javax.swing
. The rest of the lines define class
OurFrame
.
Keyword
public
indicates that this class is accessible to all other classes. The
clause
extends
JFrame
says that an instance of this class has all the components
—methods and variables— that a
JFrame
has.
We encourage you to type in this class (everything but the italicized words),
compile it, and try using it just as you did
JFrame
. Method
setTitleToOrigin
has not yet been defined, so your new class will behave in every way like a
JFrame
.
We now add the declaration of method
setTitleToOrigin
within the curly
braces
{
and
}
. Below is your first method declaration:
/**
Set the title of this instance to contain the origin of the window
*/
public void
setTitleToOrigin() {
place here statements to set the title
Style Note
13.1, 13.1.3
class names
Style Note
13.2, 13.2.4
indentation
conventions
for methods
}
Keyword
public
indicates that this method is accessible from anywhere.
Keyword
void
indicates that the method being defined is a procedure. Within the
curly braces we will place the statements that set the title to the origin. The curly
braces, together with any statements in between, constitute the
body
of the
method.
We encourage you to type this method into class
OurFrame
(everything but
the italicized words). After compiling, you can now call
ourWindow.setTitle-
ToOrigin();
(although nothing will happen yet).
We now figure out how to set the title. We want to place something like this
statement in the body of method
setTitleToOrigin
:
Search WWH ::
Custom Search