Java Reference
In-Depth Information
Indenting Code
Note the indentation used in lines 82 through 93 in Figure 5-30
on the previous page. When using a method as an argument for
another method, appropriate indentation can help make the code
more readable. It also is important to ensure that beginning and
ending parentheses and braces are indented correctly to show
where the methods start and end.
Coding the main() Method
Recall that the main() method is where execution begins when running the
compiled bytecode in Java. The main() method does not have to be at the begin-
ning of the program; indeed, it can be anywhere within the class braces and still
function properly.
In a windowed application, the main() method creates the instance of the
Frame using a call to the constructor method. Programmers typically use the letter,
f, to name their Frames, but any legal identifier is allowed. They also use the main()
method to set beginning attributes of the Frame, using methods such as those
listed in Table 5-13.
Table 5-13
Methods Used to Set Frame Attributes
METHOD NAME
PURPOSE
SAMPLE
setBounds()
Sets the screen location of the frame, using the
f.setBounds(200,200,600,300);
x and y coordinates and the width and height
setTitle()
Sets the caption in the Frame's title bar
f.setTitle(“Reserve a Party Room”);
setVisible()
Sets the visibility of the Frame
f.setVisible(true);
isResizable()
Indicates whether this Frame is Resizable by the user
if(f.isResizable());
setIconImage()
Sets the image to be displayed in the Minimize
f.setIconImage(myPic.gif);
icon for this Frame
Coding the main() Method in the Reservations Class
In the Reservations class, the Frame will be constructed with the name, f, as
shown in line 96 of Figure 5-31. The setBounds() method in line 97 will define
the location of the Frame; the setTitle() method in line 98 will assign its title bar
caption; and the setVisible() method in line 99 will cause the Frame to display.
94
public static void main ( String [] args )
95
{
96
Reservations f = new Reservations () ;
97
f.setBounds ( 200,200,600,300 ) ;
98
f.setTitle ( "Reserve a Party Room" ) ;
99
f.setVisible ( true ) ;
100
} //end of main
101
FIGURE 5-31
 
Search WWH ::




Custom Search