Java Reference
In-Depth Information
Example 21-1
is a somewhat contrived example that shows some common
javadoc
keywords
in use. The output of running this through javadoc is shown in a browser in
Figure 21-1
.
Example 21-1. JavadocDemo.java
public
public class
class
JavadocDemo
JavadocDemo
extends
extends
JPanel
{
/**
* Construct the GUI
* @throws java.lang.IllegalArgumentException if constructed on a Sunday.
*/
public
public
void
void
JavadocDemo
() {
// We create and add a pushbutton here,
// but it doesn't do anything yet.
Button b
=
new
new
Button
(
"Hello"
);
add
(
b
);
// connect Button into component
// Totally capricious example of what you should not do
iif
(
Calendar
.
getInstance
().
get
(
Calendar
.
DAY_OF_WEEK
) ==
Calendar
.
SUNDAY
) {
throw
throw new
new
IllegalArgumentException
(
"Never On A Sunday"
);
}
}
/** paint() is an AWT Component method, called when the
* component needs to be painted. This one just draws colored
* boxes in the window.
*
* @param g A java.awt.Graphics that we use for all our
* drawing methods.
*/
public
public
void
void
paint
(
Graphics g
) {
int
int
w
=
getSize
().
width
,
h
=
getSize
().
height
;
g
.
setColor
(
Color
.
YELLOW
);
g
.
fillRect
(
0
,
0
,
w
/
2
,
h
);
g
.
setColor
(
Color
.
GREEN
);
g
.
fillRect
(
w
/
2
,
0
,
w
,
h
);
g
.
setColor
(
Color
.
BLACK
);
g
.
drawString
(
"Welcome to Java"
,
50
,
50
);
}
}
The javadoc tool works fine for one class but really comes into its own when dealing with a
package or collection of packages. You can provide a package summary file for each pack-
age, which will be incorporated into the generated files. Javadoc generates thoroughly inter-
linked and crosslinked documentation, just like that which accompanies the standard JDK.