Java Reference
In-Depth Information
Notice that RAD has changed the package statement to show that the class is in c3.
10.
After the package statement, add the following two import statements:
import java.awt.Label;
import java.awt.Frame;
The import statements tell the JVM where to find the Label and Frame classes. As mentioned earlier, the JVM
automatically looks in the java.lang package. However, if you use classes stored in any other package (even packages
with the JRE), you must use an import statement to specify where the class is located. In this case, each class we
identified individually; however, you can identify all the classes in a package (for example, the awt package) with the
following statement:
import java.awt.*;
The * is a wild card symbol that indicates all classes in java.awt. This statement is easier for the programmer
to code but it does make the program a little less efficient. By not specifying a class name, the JVM will search the
package to find the class.
11.
After the displayName method header, enter the following two statements:
Label empNameLbl = new Label("My First Label");
empNameLbl.setBounds(100, 200, 75, 20);
As explained earlier, these two statements create the Label object, assign the object to empNameLbl, define text
to appear in the label, and define the label's size and location.
12.
Add 2 blank lines after the setBounds statement.
13.
On the second blank line, enter the following code to define the variable empFrame,
define and assign a Frame object to the variable, and add the label to the Employee frame.
Frame empFrame = new Frame("My First Frame");
empFrame.setBounds(10, 10, 300, 300);
empFrame.setLayout( null );
empFrame.add(empNameLbl);
empFrame.setVisible( true );
14.
Click on the System.out.println statement (to select it), then press and hold the Ctrl key,
and then press the / key (Ctrl-/) to comment the line out.
15.
Format and save the source code, then verify that there are no errors. The code should look
like Figure 3-1 .
Search WWH ::




Custom Search