Java Reference
In-Depth Information
1.
Add the following statements after the this.add statement:
this
.setTitle("New Frame Title");
this
.setLayout(
null
);
this
.setBounds(10, 10, 300, 300);
this
.setVisible(
true
);
Notice that setter methods are used to define all the frame properties (Title, Bounds, etc.).
2.
Format and save the EmpFrame source, then verify that there are no errors.
The executable code should look as follows:
package
c3;
import
java.awt.Frame;
import
java.awt.Label;
public class
EmpFrame
extends
Frame {
Label empNameLbl =
new
Label("My First Label");
public
EmpFrame(Employee emp) {
empNameLbl.setBounds(100, 150, 150, 20);
this
.add(empNameLbl);
this
.setTitle("New Frame Title");
this
.setLayout(
null
);
this
.setBounds(10, 10, 300, 300);
this
.setVisible(
true
);
}
}
3.
Run EmployeeApp.
Notice that the same old frame (empFrame) with the cut off label is displayed. This is because EmployeeApp still
creates an Employee object and Employee creates the
Frame
object empFrame. We need to change EmployeeApp so
that it creates an EmpFrame object and change Employee so that it does not create a
Frame
object.
4.
End EmployeeApp by clicking the Terminate button.
Tutorial: Using a Frame
Now let's use the frame:
1.
In EmployeeApp, add the following statement after the Employee object emp is
instantiated:
EmpFrame ef =
new
EmpFrame(emp);