Java Reference
In-Depth Information
The program to create the window shown in Figure 6-4 uses the class JFrame ; this
class is contained in the package javax.swing . Therefore, the program must include
either of the following two statements:
import javax.swing.*;
or:
import javax.swing.JFrame;
After making the minor changes in the statements described in this section, the program
to create the window shown in Figure 6-4 is as follows:
//Java program to create a window.
import javax.swing.*;
public class RectangleProgramOne extends JFrame
{
6
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
public RectangleProgramOne()
{
setTitle("Area and Perimeter of a Rectangle");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible( true );
}
public static void main(String[] args)
{
RectangleProgramOne rectProg = new RectangleProgramOne();
}
}
Notice that the named constants WIDTH and HEIGHT are declared with the modifier
private . This is because we want these named constants to be used only within the
class RectangleProgram . In general, if a named constant, variable, or method is to be
used only within the specified class , then it is declared with the modifier private .
Also, note that private is a reserved word in Java. (Chapter 8 discusses the modifier
private in detail.)
(Note that in the preceding program we have changed the name of the class to
RectangleProgramOne . This is because we have not yet added all the GUI components to
the program. After adding labels, we will call it class RectangleProgramTwo , and so on.
After adding all the necessary GUI components, we will call it class RectangleProgram .
These programs can be found with the Additional Student Files at www.cengagebrain.com.
 
Search WWH ::




Custom Search